TYVJ 1074 武士风度的牛 解题报告
真的好悲催,不知道是手生了还是蠢了,查了好久发现问题出在宏上面把变量名写错了。
这题比较简单吧,就是一个模拟,或者说BFS,广搜。。代码:
#include <stdio.h> #include <assert.h> #include <stdlib.h> #define MAX 50000 char map[150][151]; struct { int x, y; }queue[MAX]; int start, end; int cost[150][150]; int used[150][150]; void enqueue(int a, int b) { int t = (end + 1) % MAX; if(used[a][b]){ return; } used[a][b] = 1; assert(t != start); queue[end].x = a; queue[end].y = b; end = t; } void exqueue(int *a, int *b) { assert(start != end); *a = queue[start].x; *b = queue[start].y; used[*a][*b] = 0; start = (start + 1) % MAX; } int main(int argc, char **argv) { int i, j; int m, n; int x, y; int a, b; scanf("%d%d\n", &m, &n); for(i = 0; i < n; i++){ for(j = 0; j < m; j++){ cost[i][j] = 0xFFFFFFF; } } for(i = 0; i < n; i++){ scanf("%s\n", map[i]); enqueue(map[i][0], map[i][1]); for(j = 0; j < m; j++){ if(map[i][j] == 'K'){ cost[i][j] = 0; map[i][j] = '.'; enqueue(i, j); } if(map[i][j] == 'H'){ map[i][j] = '.'; a = i, b = j; } } } while(start != end){ exqueue(&x, &y); #define can(i, k) ((i) >= 0 && (i) < (k)) #define deal(a, b) if(can(a, n) && can(b, m) && map[(a)][(b)] == '.' && cost[a][b] > cost[x][y] + 1){\ cost[a][b] = cost[x][y] + 1;\ enqueue((a), (b));\ } deal(x + 2, y - 1); deal(x + 2, y + 1); deal(x - 2, y - 1); deal(x - 2, y + 1); deal(x + 1, y - 2); deal(x + 1, y + 2); deal(x - 1, y - 2); deal(x - 1, y + 2); } printf("%d\n", cost[a][b]); return 0; }