hdu1429 胜利大逃亡(续)--BFS
2016-07-21 12:09
351 查看
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1429
一:分析
定义一个三维数组,标记该点是否走过,其中第三维代表在该路径上所获钥匙的标记。
二:AC代码
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
#include<iostream>
#include<algorithm>
#include<queue>
#include<cctype>
using namespace std;
struct Node
{
int x, y;
int step;
int state;
Node(){}
Node(int x, int y, int step, int state) :x(x), y(y), step(step), state(state) {}
};
int dir[4][2] = { { 0, 1 },{ 0, -1 },{ 1, 0 },{ -1, 0 } };
char s[25][25];
bool vis[25][25][1 << 10];
int n, m, t;
int get(char c)
{
return c - (isupper(c) ? 'A' : 'a');
}
bool check(int x, int y)
{
if (x < 0 || x >= n || y < 0 || y >= m || s[x][y] == '*')
return false;
return true;
}
int bfs(int x, int y)
{
queue<Node> Q;
vis[x][y][0] = 1;
Q.push(Node(x, y, 0, 0));
while (!Q.empty())
{
Node cur = Q.front();
Q.pop();
if (cur.step >= t - 1)
break;
for (int i = 0; i < 4; i++)
{
Node temp = cur;
temp.x += dir[i][0];
temp.y += dir[i][1];
temp.step++;
if (check(temp.x, temp.y) == 0 || vis[temp.x][temp.y][temp.state])
continue;
if (isupper(s[temp.x][temp.y]) && !(temp.state&(1 << get(s[temp.x][temp.y]))))//遇到门,没有钥匙,不走
continue;
if (islower(s[temp.x][temp.y]))//找到钥匙
temp.state |= (1 << get(s[temp.x][temp.y]));
if (s[temp.x][temp.y] == '^')
return temp.step;
if (!vis[temp.x][temp.y][temp.state])
{
vis[temp.x][temp.y][temp.state] = 1;
Q.push(temp);
}
}
}
return -1;
}
int main()
{
int x, y;
while (~scanf("%d%d%d", &n, &m, &t))
{
for (int i = 0; i < n; i++)
{
getchar();
for (int j = 0; j < m; j++)
{
scanf("%c", &s[i][j]);
if (s[i][j] == '@')
{
x = i;
y = j;
}
}
}
memset(vis, 0, sizeof(vis));
printf("%d\n", bfs(x, y));
}
return 0;
}
一:分析
定义一个三维数组,标记该点是否走过,其中第三维代表在该路径上所获钥匙的标记。
二:AC代码
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
#include<iostream>
#include<algorithm>
#include<queue>
#include<cctype>
using namespace std;
struct Node
{
int x, y;
int step;
int state;
Node(){}
Node(int x, int y, int step, int state) :x(x), y(y), step(step), state(state) {}
};
int dir[4][2] = { { 0, 1 },{ 0, -1 },{ 1, 0 },{ -1, 0 } };
char s[25][25];
bool vis[25][25][1 << 10];
int n, m, t;
int get(char c)
{
return c - (isupper(c) ? 'A' : 'a');
}
bool check(int x, int y)
{
if (x < 0 || x >= n || y < 0 || y >= m || s[x][y] == '*')
return false;
return true;
}
int bfs(int x, int y)
{
queue<Node> Q;
vis[x][y][0] = 1;
Q.push(Node(x, y, 0, 0));
while (!Q.empty())
{
Node cur = Q.front();
Q.pop();
if (cur.step >= t - 1)
break;
for (int i = 0; i < 4; i++)
{
Node temp = cur;
temp.x += dir[i][0];
temp.y += dir[i][1];
temp.step++;
if (check(temp.x, temp.y) == 0 || vis[temp.x][temp.y][temp.state])
continue;
if (isupper(s[temp.x][temp.y]) && !(temp.state&(1 << get(s[temp.x][temp.y]))))//遇到门,没有钥匙,不走
continue;
if (islower(s[temp.x][temp.y]))//找到钥匙
temp.state |= (1 << get(s[temp.x][temp.y]));
if (s[temp.x][temp.y] == '^')
return temp.step;
if (!vis[temp.x][temp.y][temp.state])
{
vis[temp.x][temp.y][temp.state] = 1;
Q.push(temp);
}
}
}
return -1;
}
int main()
{
int x, y;
while (~scanf("%d%d%d", &n, &m, &t))
{
for (int i = 0; i < n; i++)
{
getchar();
for (int j = 0; j < m; j++)
{
scanf("%c", &s[i][j]);
if (s[i][j] == '@')
{
x = i;
y = j;
}
}
}
memset(vis, 0, sizeof(vis));
printf("%d\n", bfs(x, y));
}
return 0;
}
相关文章推荐
- jQuery对象和JavaScript对象之间的转换!jQuery党情何以堪啊!
- 制作nginx和php的rpm包
- 官网下载kettle
- HHVM
- JS中的原型类
- Android静态安全检测 -> logcat可能泄露隐私敏感信息
- MFC中的Document-View结构
- 将openfire源码部署到IDEA中
- Python学习进程(8)字符串內建函数
- POJ 3278 Catch That Cow(BFS)
- native invoke js和js invoke native
- 274. H-Index(难)
- SUI的无限下拉和分页效果的实现
- iPhone屏幕尺寸、分辨率及适配
- 按照原计划,继续全力以赴
- ListView图片加载错乱02
- PHP 解决xml文件 xml_parse — 开始解析一个 XML 文档
- CF620C - Pearls in a Row 数据结构map映射
- NSProcessInfo获取进程信息,UIDevice获取设信息
- Xcode 7 缺少 *.dylib库的解决方法