#include using namespace std; struct w{ string val; int index; }; int t, n; w words[10005]; char pravci_dir[4] = {'R', 'U', 'L', 'D'}; char pravci_x[4] = {1, 0, -1, 0}; char pravci_y[4] = {0, -1, 0, 1}; bool stopRec = false; int najduzaRecIndex; string startDirections[10005]; int startPositions[10005][2]; char tabela[1005][1005]; int startLeftMostX, startRightMostX, startTopMostY, startBottomMostY; bool starUsed; void subTaskX(string directions, int locX, int locY, int wordIndex, int charIndex, int leftMostX = 0, int rightMostX = 0, int topMostY = 0, int bottomMostY = 0) { if (locX < leftMostX) { leftMostX = locX; } if (locX > rightMostX) { rightMostX = locX; } if (locY < topMostY) { topMostY = locY; } if (locY > bottomMostY) { bottomMostY = locY; } if (charIndex == words[wordIndex].val.size()) { stopRec = true; startLeftMostX = leftMostX; startRightMostX = rightMostX; startTopMostY = topMostY; startBottomMostY = bottomMostY; startDirections[words[wordIndex].index] = directions; /* for (int i = 0; i < 1000; ++i) { for (int j = 0; j < 1000; ++j) { startTabela[i][j] = tabela[i][j]; } }*/ return; } for (int i = 0; i < 4; ++i) { if (stopRec == true) { return; } if (locX+pravci_x[i] > 999 || locX+pravci_x[i] < 0 || locY+pravci_y[i] > 999 || locY+pravci_y[i] < 0) { continue; } if (tabela[locX+pravci_x[i]][locY+pravci_y[i]] == words[wordIndex].val[charIndex] || tabela[locX+pravci_x[i]][locY+pravci_y[i]] == '*') { directions.push_back(pravci_dir[i]); subTaskX(directions, locX+pravci_x[i], locY+pravci_y[i], wordIndex, charIndex+1, leftMostX, rightMostX, topMostY, bottomMostY); if (stopRec == true) { return; } directions.pop_back(); } else if (tabela[locX+pravci_x[i]][locY+pravci_y[i]] != 0 && starUsed == false) { char startVal = tabela[locX+pravci_x[i]][locY+pravci_y[i]]; starUsed = true; tabela[locX+pravci_x[i]][locY+pravci_y[i]] = '*'; directions.push_back(pravci_dir[i]); subTaskX(directions, locX+pravci_x[i], locY+pravci_y[i], wordIndex, charIndex+1, leftMostX, rightMostX, topMostY, bottomMostY); if (stopRec == true) { return; } starUsed = false; tabela[locX+pravci_x[i]][locY+pravci_y[i]] = startVal; directions.pop_back(); } } for (int i = 0; i < 4; ++i) { if (stopRec == true) { return; } if (locX+pravci_x[i] > 999 || locX+pravci_x[i] < 0 || locY+pravci_y[i] > 999 || locY+pravci_y[i] < 0) { continue; } if (tabela[locX+pravci_x[i]][locY+pravci_y[i]] == 0) { directions.push_back(pravci_dir[i]); tabela[locX+pravci_x[i]][locY+pravci_y[i]] = words[wordIndex].val[charIndex]; subTaskX(directions, locX+pravci_x[i], locY+pravci_y[i], wordIndex, charIndex+1, leftMostX, rightMostX, topMostY, bottomMostY); if (stopRec == true) { return; } directions.pop_back(); tabela[locX+pravci_x[i]][locY+pravci_y[i]] = 0; } } } bool wordSortFunc(w word1, w word2) { return word1.val > word2.val; } chrono::steady_clock::time_point beginTime; chrono::steady_clock::time_point endTime; int main() { beginTime = chrono::steady_clock::now(); freopen("crossword.in", "r", stdin); freopen("crossword.out", "w", stdout); scanf("%d %d", &t, &n); for (int i = 0; i < n; ++i) { cin >> words[i].val; words[i].index = i; if (words[najduzaRecIndex].val.size() < words[i].val.size()) { najduzaRecIndex = i; } } startPositions[najduzaRecIndex][0] = 500; startPositions[najduzaRecIndex][1] = 500; string emptyDirections; starUsed = true; tabela[500][500] = '*'; subTaskX(emptyDirections, 500, 500, najduzaRecIndex, 1, 500, 500, 500, 500); sort(words, words+n, wordSortFunc); for (int i = 0; i < n; ++i) { if (words[i].index == najduzaRecIndex) continue; stopRec = false; for (int j = startLeftMostX; j <= startRightMostX; ++j) { for (int k = startTopMostY; k <= startBottomMostY; ++k) { if (tabela[j][k] == words[i].val[0] || tabela[j][k] == '*') { string emptyDirections; subTaskX(emptyDirections, j, k, i, 1, startLeftMostX, startRightMostX, startTopMostY, startBottomMostY); //endTime = chrono::steady_clock::now(); //if (chrono::duration_cast(endTime - beginTime).count() < 4) //{ //cout << chrono::duration_cast(endTime - beginTime).count()<< endl; //} if (stopRec == true) { startPositions[words[i].index][0] = j; startPositions[words[i].index][1] = k; break; } } } if (stopRec == true) { break; } } if (stopRec == false) { for (int j = 0; j < 1000; ++j) { for (int k = 0; k < 1000; ++k) { if (tabela[j][k] == 0) { string emptyDirections; tabela[j][k] = words[i].val[0]; subTaskX(emptyDirections, j, k, i, 1, startLeftMostX, startRightMostX, startTopMostY, startBottomMostY); if (stopRec == true) { startPositions[words[i].index][0] = j; startPositions[words[i].index][1] = k; break; } else tabela[j][k] = 0; } } if (stopRec == true) { break; } } } } printf("1000 1000\n"); for (int i = 0; i < 1000; ++i) { for (int j = 0; j < 1000; ++j) { if (tabela[j][i] == 0) { putchar('#'); } else { putchar(tabela[j][i]); } } putchar('\n'); } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (words[j].index == i) { printf("%d %d ", startPositions[i][0], startPositions[i][1]); cout << startDirections[i] << endl; break; } } /* cout << "vozamo se direkcijama" << endl; int x = startPositions[i][0]; int y = startPositions[i][1]; cout << startTabela[x][y] << " - ocekuje se " << words[i].val[0] << endl; for (int j = 0; j < startDirections[i].size(); ++j) { if (startDirections[i][j] == 'R') { ++x; } else if (startDirections[i][j] == 'L') { --x; } else if (startDirections[i][j] == 'U') { --y; } else if (startDirections[i][j] == 'D') { ++y; } cout << startTabela[x][y] << " - ocekuje se " << words[i].val[j+1] << endl; if (startTabela[x][y] != words[i].val[j+1]) { cout << "STOJ!" << endl; break; } }*/ } return 0;