#include #include #include class cell { public: int left; int right; int top; int bottom; }; using namespace std; int main() { cell *maze = NULL; int r, c, i, index; int rows, cols; string line, word; vector fields; int linenum=0; // Read in the maze file. while (getline(cin, line)) { //read each line of input linenum++; fields.clear(); i=0; while (i < line.length()) { //put fields of line into a vector of strings€ while (i 0) fields.push_back(word); } if (fields.size() > 0) { if (fields[0] == "MAZE") { if (fields.size() != 4 || fields[2] != "X") { fprintf(stderr, "Line %d: MAZE line should be 'MAZE rows X cols'\n", linenum); exit(1); } if (sscanf(fields[1].c_str(), "%d", &rows) != 1 || rows <= 0) { fprintf(stderr, "Line %d: MAZE line should be 'MAZE rows X cols'\n", linenum); exit(1); } if (sscanf(fields[3].c_str(), "%d", &cols) != 1 || rows <= 0) { fprintf(stderr, "Line %d: MAZE line should be 'MAZE rows X cols'\n", linenum); exit(1); } if (maze != NULL) { fprintf(stderr, "Line %d: file should have only one MAZE line\n", linenum); exit(1); } maze = new cell[rows*cols]; for (i = 0; i < rows*cols; i++) { maze[i].left = 0; maze[i].right = 0; maze[i].top = 0; maze[i].bottom = 0; } } else if (fields[0] == "CELL") { if (maze == NULL) { fprintf(stderr, "Line %d: No MAZE line.\n", linenum); exit(1); } if (fields.size() < 3 || fields.size() > 7) { fprintf(stderr, "Line %d: CELL line should be 'CELL r c [T B L R]'\n", linenum); exit(1); } if (sscanf(fields[1].c_str(), "%d", &r) != 1 || sscanf(fields[2].c_str(), "%d", &c) != 1) { fprintf(stderr, "Line %d: CELL line should be 'CELL r c [T B L R]'\n", linenum); exit(1); } if (r < 0 || r >= rows) { fprintf(stderr, "Line %d: bad row number\n", linenum); exit(1); } if (c < 0 || c >= cols) { fprintf(stderr, "Line %d: bad column number\n", linenum); exit(1); } index = r * cols + c; for (i = 3; i < fields.size(); i++) { if (fields[i] == "L") { maze[index].left = 1; } else if (fields[i] == "R") { maze[index].right = 1; } else if (fields[i] == "T") { maze[index].top = 1; } else if (fields[i] == "B") { maze[index].bottom = 1; } else { fprintf(stderr, "Line %d: CELL line should be 'CELL r c [T B L R]'\n", linenum); exit(1); } } // printf("%d %d %d %d %d %d\n", r, c, maze[index].left, // maze[index].right, // maze[index].top, // maze[index].bottom); } else if (fields[0][0] == '#') { } else { fprintf(stderr, "Line %d: Lines should be MAZE or CELL\n", linenum); exit(1); } } } //end while loop that reads lines of input if (maze == NULL) return 0; // Double check to make sure that all cells with shared walls are // correct. index = 0; for (r = 0; r < rows; r++) { for (c = 0; c < cols; c++) { // Check top/bottom if (r > 0 && maze[index-cols].bottom != maze[index].top) { fprintf(stderr, "Cell (%d,%d) top does not match (%d,%d) bottom\n", r, c, r-1, c); exit(1); } // Check left/right if (c > 0 && maze[index].left != maze[index-1].right) { fprintf(stderr, "Cell (%d,%d) left does not match (%d,%d) right\n", r, c, r, c-1); exit(1); } index++; } } // Print it out. // Print top printf("_"); for (c = 0; c < cols; c++) { printf("%s", (maze[c].top) ? "_" : " "); printf("_"); } printf("\n"); index = 0; for (r = 0; r < rows; r++) { for (c = 0; c < cols; c++) { if (c == 0) printf("%s", (maze[index].left) ? "|" : " "); printf("%s", (maze[index].bottom) ? "_" : " "); if (maze[index].bottom || (c < cols-1 && maze[index+1].bottom)) { printf("%s", (maze[index].right) ? "|" : "_"); } else { printf("%s", (maze[index].right) ? "|" : " "); } index++; } printf("\n"); } printf("\n"); return 0; }