📄 1140.cpp
字号:
/* This Code is Submitted by wywcgs for Problem 1140 on 2006-02-22 at 15:01:24 */
#include <cstdio>
#include <queue>
#include <climits>
#include <algorithm>
using namespace std;
typedef unsigned int uint;
const int MAX = 128;
const int DIR[][2] = { { -1, 0 }, { 0, -1 }, { 1, 0 }, { 0, 1 } };
char map[MAX][MAX];
int n, m;
uint step[MAX][MAX];
queue<int> Q;
inline bool legal(int x, int y) { return (x >= 0 && x <= n && y >= 0 && y <= m); }
uint play(int, int, int, int);
void update(int, int);
int main()
{
int i, case_no, pair_no;
for(case_no = 1; scanf("%d %d%*c", &m, &n) != EOF && n != 0; case_no++) {
memset(map, 0, sizeof(map)); n++; m++;
for(i = 1; i < n; i++) gets(map[i]+1);
printf("Board #%d:\n", case_no);
int bx, by, ex, ey;
for(pair_no = 1; scanf("%d %d %d %d", &by, &bx, &ey, &ex) != EOF && bx != 0; pair_no++) {
printf("Pair %d: ", pair_no);
uint seg = play(bx, by, ex, ey);
if(seg == UINT_MAX) printf("impossible.\n");
else printf("%u segments.\n", seg);
}
putchar('\n');
}
return 0;
}
uint play(int bx, int by, int ex, int ey)
{
while(!Q.empty()) Q.pop();
memset(step, -1, sizeof(step));
step[bx][by] = 0; update(bx, by);
while(!Q.empty() && step[ex][ey] == UINT_MAX) {
int p = Q.front(); Q.pop();
update(p>>8, p&127);
}
return step[ex][ey];
}
void update(int x, int y)
{
int i, j, lmt = max(n, m);
for(i = 0; i < 4; i++)
for(j = 1; j <= lmt; j++) {
int cx = x+j*DIR[i][0], cy = y+j*DIR[i][1];
if(!legal(cx, cy) || step[cx][cy] <= step[x][y]) break;
uint b = step[cx][cy]; step[cx][cy] = step[x][y] + 1;
if(map[cx][cy] == 'X') break;
else if(b == UINT_MAX) Q.push((cx<<8)|cy);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -