hdu2216
来源:互联网 发布:好的淘宝女装店铺推荐 编辑:程序博客网 时间:2024/06/08 15:58
/*
* File: hdu2216.cpp
* Author: chenjiang
*
* Created on 2010年5月9日, 下午7:33
*/
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <math.h>
using namespace std;
#define _max 25
#define inf 214748364
char mapmap[_max][_max];
int g[_max][_max][_max][_max];
struct node {
int sx, sy;
int zx, zy;
int step;
} start;
int next1[4][2] = {
{0, -1},
{0, 1},
{1, 0},
{-1, 0},
};
int n, m;
void Init() {
int i, j, k, s;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
for (k = 0; k < n; k++) {
for (s = 0; s < m; s++) {
g[i][j][k][s] = inf;
}
}
}
}
}
void bfs() {
int i, j, k, s;
int ans = inf;
Init();
start.step = 0;
queue<node>Q;
Q.push(start);
while (!Q.empty()) {
node temp = Q.front();
Q.pop();
if (temp.step >= g[temp.zx][temp.zy][temp.sx][temp.sy]) {
continue;
}
g[temp.zx][temp.zy][temp.sx][temp.sy] = temp.step;
if (abs(temp.zx - temp.sx) + abs(temp.zy - temp.sy) <= 1) {
if (temp.step < ans) {
ans = temp.step;
}
continue;
}
for (i = 0; i < 4; i++) {
node t;
int zx, zy, sx, sy;
zx = temp.zx + next1[i][0];
zy = temp.zy + next1[i][1];
if (zx >= 0 && zx < n && zy >= 0 && zy < m && mapmap[zx][zy] != 'X') {
sx = temp.sx - next1[i][0];
sy = temp.sy - next1[i][1];
if (sx >= 0 && sx < n && sy >= 0 && sy < m && mapmap[sx][sy] != 'X') {
t.step = temp.step + 1;
t.zx = zx;
t.zy = zy;
t.sx = sx;
t.sy = sy;
Q.push(t);
} else {
t.step = temp.step + 1;
t.zx = zx;
t.zy = zy;
t.sx = temp.sx;
t.sy = temp.sy;
Q.push(t);
}
}
}
}
if (ans == inf) {
cout << "Bad Luck!" << endl;
} else {
cout << ans << endl;
}
}
/*
*
*/
int main(int argc, char** argv) {
int i, j;
while (cin >> n >> m) {
for (i = 0; i < n; i++) {
cin >> mapmap[i];
for (j = 0; j < m; j++) {
if (mapmap[i][j] == 'Z') {
start.zx = i;
start.zy = j;
}
if (mapmap[i][j] == 'S') {
start.sx = i;
start.sy = j;
}
}
}
bfs();
}
return (EXIT_SUCCESS);
}
- hdu2216
- hdu2216
- HDU2216:Game III(BFS)
- hdu2216之BFS
- HDU2216:Game III(BFS)
- HDU2216 Game III
- hdu2216 Game III 【广搜】
- hdu2216 Game III--BFS(待解决)
- 开源Erlang真的能成为下一代Java语言吗?
- puj 图论分类
- 测试中白盒测试与黑盒测试
- 评我的事业心
- omnimeeting视频会议程序编译(vs2008)
- hdu2216
- omnimeeting在vs2008中的编译
- 关于国内软件行业发展状况的调查
- vc 添加自定义消息及消息处理函数分5步
- Audio Sampling
- IBM大中华区副总裁解读软件行业发展状况
- CButtonST的用法详解!
- 增加虚拟机硬盘分区大小
- VSS使用手册