1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
using namespace std; int fx[4] = { 0,1,0,-1 }; int fy[4] = { 1,0,-1,0 }; int land[105][105]; int height, wide, start_x, start_y, end_x, end_y; bool visit[105][105]; int flag = 0; struct position { int x; int y; int next = 0; }; vector<position>v; bool judge_size(int x, int y) { if (x < 0 || y < 0) return false; else if (x > height || y > wide) return false; else if (visit[x][y] == 1) return false; else if (land[x][y] == 1) return false; else return true; } int DFS(int x, int y) { int temp_x, temp_y, i, step; visit[x][y] = 1; if (!v.empty()) { int last_x = v.back().x; int last_y = v.back().y; if (y - last_y == 1) step = 1; else if (x - last_x == 1) step = 2; else if (y - last_y == -1) step = 3; else if (x - last_x == -1) step = 4; v.back().next = step; } position a; a.x = x; a.y = y; v.push_back(a); if (x == end_x && y == end_y) { flag = 1; return 1; } for (i = 0; i < 4; i++) { temp_x = x + fx[i]; temp_y = y + fy[i]; //if (judge_size(temp_x, temp_y)) if (temp_x < 0 || temp_y < 0 || temp_x > height || temp_y > wide || visit[temp_x][temp_y] == 1 || land[temp_x][temp_y] == 1) continue; else { DFS(temp_x, temp_y); if (flag == 1) return 1; } } visit[x][y] = 0; v.pop_back(); if (x == start_x && y == start_y && i >= 4) return 0; } int main() { //cin >> height >> wide; //cin >> start_x >> start_y; //cin >> end_x >> end_y; //scanf_s("%d %d", &height, &wide); scanf("%d %d", &height, &wide); //scanf_s("%d %d", &start_x, &start_y); scanf("%d %d", &start_x, &start_y); //scanf_s("%d %d", &end_x, &end_y); scanf("%d %d", &end_x, &end_y); for (int i = 0; i <= wide + 1; i++) { land[0][i] = 1; land[height + 1][i] = 1; } for (int i = 0; i <= height + 1; i++) { land[i][0] = 1; land[i][wide + 1] = 1; } for (int i = 1; i <= height; i++) { for (int j = 1; j <= wide; j++) { //cin >> land[i][j]; //scanf_s("%d", &land[i][j]); scanf("%d", &land[i][j]); } } if (!DFS(start_x, start_y)) { //cout << "no" << endl; printf("no\n"); return 0; } /*int step = 0; for (int i = 1; i <= height; i++) { for (int j = 1; j <= wide; j++) { cout << visit[i][j]; if (visit[i][j] == 1) { if (visit[i][j + 1] == 1) step = 1; else if (visit[i + 1][j] == 1) step = 2; else if (visit[i][j - 1] == 1) step = 3; else if (visit[i - 1][j] == 1) step = 4; } } cout << endl; }*/ if (!v.empty()) { v.back().next = 1; } //cout << '(' << v[0].x << ',' << v[0].y << ',' << v[0].next << ')'; printf("(%d,%d,%d)", v[0].x, v[0].y, v[0].next); for (int i = 1; i < v.size(); i++) { //cout << ",(" << v[i].x << ',' << v[i].y << ',' << v[i].next << ')'; printf(",(%d,%d,%d)", v[i].x, v[i].y, v[i].next); } return 0; }
|