#include<stdio.h> #include<stdlib.h> int mark[12][12], maze[12][12]; int move[4][2] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } }; int m, n; struct { int x[100], y[100]; int top; }path; int SeekPath(int x, int y); void main() { int i,j; scanf("%d%d", &m, &n); for (i = 1; i <= m;i++) for (j = 1; j <= n; j++) scanf("%d", &maze[i][j]); for (i = 0; i <= m + 1; i++) { maze[i][0] = 1; maze[i][n + 1] = 1; } for (j = 1; j <= n; j++) { maze[0][j] = 1; maze[m + 1][j] = 1; } for (i = 0; i <= m + 1;i++) for (j = 1; j <= n + 1; j++) mark[i][j] = 0; path.top = 0; if (SeekPath(1, 1)) { path.x[path.top] = 1; path.y[path.top] = 1; path.top++; } j = 1; for (i = path.top - 1; i >= 0; i--) { printf("(%d,%d)", path.x[i], path.y[i]); if ((j++) % 5 == 0) printf(" "); } system("pause"); } int SeekPath(int x, int y) { int i, g, h; mark[x][y] = 1; if (x == m&&y == n) return(1); for (i = 0; i <=4; i++) { g = x + move[i][0]; h = y + move[i][1]; if ((maze[g][h] == 0) && (mark[g][h] == 0)) { mark[g][h] = 1; if (SeekPath(g, h)) { path.x[path.top] = g; path.y[path.top] = h; path.top++; return(1); } } } return(0); }
struct { int x, y; int px, py; }que[100],s[100]; int h, t; int top; int SeekPath(int x, int y) { int cx, cy, cpx, cpy, dx, dy, i; que[t].x = x; que[t].y = y; que[t].px = 0; que[t].py = 0; t++; s[top].x = x; s[top].y = y; s[top].px = 0; s[top].py = 0; top++; while (h != t) { cx = que[h].x; cy = que[h].y; cpx = que[h].px; cpy = que[h].py; h++; if (cx == m&&cy == n) { break; } s[top].x = cx; s[top].y = cy; s[top].px = cpx; s[top].py = cpy; top++; for (i = 0; i < 4; i++) { dx = cx + move[i][0]; dy = cy + move[i][1]; if (maze[dx][dy] == 0 && mark[dx][dy] == 0) { mark[dx][dy] = 1; que[t].x = dx; que[t].y = dy; que[t].px = cx; que[t].py = cy; t++; } } } printf("%d,%d ", cx, cy); while (top != 0) { if (cpy == s[top].x&&cpy == s[top].y) { printf("%d,%d ", cpx, cpy); cpx = s[top].px; cpy = s[top].py; } top--; } return(0); }
#include<stdio.h> #define M 4 #define N 5 int move[8][2] = { { -1, -2 }, { -1, 2 }, { -2, 1 }, { -2, -1 }, { 2, 1 }, { 2, -1 }, { 1, 2 }, { 1, -2 } }; int mark[M][N] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; int SeekPath(int x, int y); int sx, sy, num; int main() { scanf("%d%d", &sx, &sy); sx--; sy--; num = 0; mark[sx][sy] = 1; SeekPath(sx, sy); printf("%d ", num); return 0; } int SeekPath(int x, int y) { int i, dx, dy; for (i = 0; i < 8; i++) { dx = x + move[i][0]; dy = y + move[i][1]; if (dx >= 0 && dy >= 0 && dx < M&&dy < N&&mark[dx][dy] == 0) { mark[dx][dy] = 1; SeekPath(dx, dy); mark[dx][dy] = 0; } if (dx == sx&&dy == sy) num++; } return(0); }
// //struct QUETP //{ // char que[QN][21]; // int c[QN]; // int front, rear; //}; #include<stdio.h> #include<string.h> #define QN 1000 struct QUETP { char que[QN][21]; int c[QN]; int front, rear; }que; void initqueue(struct QUETP *q) { q->front = 0; q->rear = 0; } int enqueue(struct QUETP*q, char*str, int m) { if (q->front == (q->rear + 1) % QN) return(0); strcpy(q->que[q->rear], str); q->c[q->rear] = m; q->rear = (q->rear + 1) % QN; return(1); } int delqueue(struct QUETP*q, char*str, int *m) { if (q->front == q->rear) return(0); strcpy(str, q->que[q->front]); *m = q->c[q->front]; q->front = (q->front + 1) % QN; return(1); } int emptyqueue(struct QUETP q) { if (q.front == q.rear) return(1); else return(0); } int findstr(char*strs, int k, char*substr) { int len, slen, i, j; len = strlen(strs); slen = strlen(substr); for (i = k; i < len - slen + 1; i++) { for (j = 0; j < slen; j++) { if (strs[j + i] != substr[j]) break; } if (j == slen) return(i); } return(-1); } void replstr(char*strs, int pos, char*substr1, char*substr2, char *tarstr) { int len; len = strlen(substr1); strcpy(tarstr, strs); tarstr[pos] = '