hdu 1242 Rescue
2016-07-20 15:43
549 查看
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
int n, m, sx, sy, judge[210][210], d[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
char map[210][210];
struct node
{
int x, y, time;
friend bool operator < (node a, node b)
{
return a.time > b.time;
}
};
int bfs()
{
node now, next;
priority_queue<node>q;
while(!q.empty())
q.pop();
now.x = sx;
now.y = sy;
now.time = 0;
q.push(now);
judge[now.x][now.y] = 1;
while(!q.empty())
{
now = q.top();
q.pop();
if(map[now.x][now.y] == 'r') return now.time;
for(int i = 0; i < 4; i++)
{
next.x = now.x + d[i][0];
next.y = now.y + d[i][1];
if(next.x >= 0 && next.x < n && next.y >= 0 && next.y < m && !judge[next.x][next.y] && map[next.x][next.y]!='#')
{
if(map[next.x][next.y] == 'x') next.time = now.time + 2;
else
next.time = now.time + 1;
judge[next.x][next.y] = 1;
q.push(next);
}
}
}
return -1;
}
int main()
{
while(scanf("%d%d", &n, &m)!=EOF)
{
if(n==0&&m==0) break;
for(int i=0; i<n; i++)
scanf("%s",map[i]);
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
if(map[i][j] == 'a')
{
sx = i;
sy = j;
}
}
}
memset(judge, 0, sizeof(judge));
int f = bfs();
if(f == -1)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n", f);
}
return 0;
}
区别
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
int n, m, sx, sy, judge[210][210], d[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
char map[210][210];
struct node
{
int x, y, time;
friend bool operator < (node a, node b)
{
return a.time > b.time;
}
};
int bfs()
{
node now, next;
priority_queue<node>q;
while(!q.empty())
q.pop();
now.x = sx;
now.y = sy;
now.time = 0;
q.push(now);
judge[now.x][now.y] = 1;
while(!q.empty())
{
now = q.top();
q.pop();
if(map[now.x][now.y] == 'r') return now.time;
for(int i = 0; i < 4; i++)
{
next.x = now.x + d[i][0];
next.y = now.y + d[i][1];
if(next.x >= 0 && next.x < n && next.y >= 0 && next.y < m && !judge[next.x][next.y] && map[next.x][next.y]!='#')
{
if(map[next.x][next.y] == 'x') next.time = now.time + 2;
else
next.time = now.time + 1;
judge[next.x][next.y] = 1;
q.push(next);
}
}
}
return -1;
}
int main()
{
while(scanf("%d%d", &n, &m)!=EOF)
{
if(n==0&&m==0) break;
getchar();
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
scanf("%c", &map[i][j]);
if(map[i][j] == 'a')
{
sx = i;
sy = j;
}
}
getchar();
}
memset(judge, 0, sizeof(judge));
int f = bfs();
if(f == -1)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n", f);
}
return 0;
}
#include<queue>
#include<string.h>
using namespace std;
int n, m, sx, sy, judge[210][210], d[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
char map[210][210];
struct node
{
int x, y, time;
friend bool operator < (node a, node b)
{
return a.time > b.time;
}
};
int bfs()
{
node now, next;
priority_queue<node>q;
while(!q.empty())
q.pop();
now.x = sx;
now.y = sy;
now.time = 0;
q.push(now);
judge[now.x][now.y] = 1;
while(!q.empty())
{
now = q.top();
q.pop();
if(map[now.x][now.y] == 'r') return now.time;
for(int i = 0; i < 4; i++)
{
next.x = now.x + d[i][0];
next.y = now.y + d[i][1];
if(next.x >= 0 && next.x < n && next.y >= 0 && next.y < m && !judge[next.x][next.y] && map[next.x][next.y]!='#')
{
if(map[next.x][next.y] == 'x') next.time = now.time + 2;
else
next.time = now.time + 1;
judge[next.x][next.y] = 1;
q.push(next);
}
}
}
return -1;
}
int main()
{
while(scanf("%d%d", &n, &m)!=EOF)
{
if(n==0&&m==0) break;
for(int i=0; i<n; i++)
scanf("%s",map[i]);
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
if(map[i][j] == 'a')
{
sx = i;
sy = j;
}
}
}
memset(judge, 0, sizeof(judge));
int f = bfs();
if(f == -1)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n", f);
}
return 0;
}
区别
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
int n, m, sx, sy, judge[210][210], d[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
char map[210][210];
struct node
{
int x, y, time;
friend bool operator < (node a, node b)
{
return a.time > b.time;
}
};
int bfs()
{
node now, next;
priority_queue<node>q;
while(!q.empty())
q.pop();
now.x = sx;
now.y = sy;
now.time = 0;
q.push(now);
judge[now.x][now.y] = 1;
while(!q.empty())
{
now = q.top();
q.pop();
if(map[now.x][now.y] == 'r') return now.time;
for(int i = 0; i < 4; i++)
{
next.x = now.x + d[i][0];
next.y = now.y + d[i][1];
if(next.x >= 0 && next.x < n && next.y >= 0 && next.y < m && !judge[next.x][next.y] && map[next.x][next.y]!='#')
{
if(map[next.x][next.y] == 'x') next.time = now.time + 2;
else
next.time = now.time + 1;
judge[next.x][next.y] = 1;
q.push(next);
}
}
}
return -1;
}
int main()
{
while(scanf("%d%d", &n, &m)!=EOF)
{
if(n==0&&m==0) break;
getchar();
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
scanf("%c", &map[i][j]);
if(map[i][j] == 'a')
{
sx = i;
sy = j;
}
}
getchar();
}
memset(judge, 0, sizeof(judge));
int f = bfs();
if(f == -1)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n", f);
}
return 0;
}
相关文章推荐
- easyUI的使用
- HUE配置HBase
- UESTC OJ1219 Ba Gua Zhen
- Unique Paths II
- 334. Increasing Triplet Subsequence
- UIStackView 的使用
- String、StringBuffer、StringBuilder的区别
- EF CreateQuery()的命名空间
- easyUI 获取记录数 页数 当前页
- 去掉UICollectionView 在 reloadItems时默认附加的隐式fade动画。
- Unique Paths
- Windows下设备GUID常识
- LeetCode Unique Binary Search Trees动态规划方法详解
- 安装Flash Builder4.6遇到的问题
- iOS开发UI篇-自定义CALayer
- UI阅读笔记
- iOS UILabel设置行间距
- iOS开发从入门到精通-- UIProgressView进度条&UISlider滑动条
- 如何做好UI设计
- UITableView中cell上按钮获取所选cell