BFS:森林着火
2016-07-20 14:55
302 查看
Description
input
input.txt
output
output.txt
After a terrifying forest fire in Berland a forest rebirth program was carried out. Due to it N rows with M trees each were planted and the rows were so neat that one could map it on a system of coordinates so that the j-th tree in the i-th row would have the coordinates of (i, j). However a terrible thing happened and the young forest caught fire. Now we must find the coordinates of the tree that will catch fire last to plan evacuation.
The burning began in K points simultaneously, which means that initially K trees started to burn. Every minute the fire gets from the burning trees to the ones that aren’t burning and that the distance from them to the nearest burning tree equals to 1.
Find the tree that will be the last to start burning. If there are several such trees, output any.
Input
The first input line contains two integers N, M (1 ≤ N, M ≤ 2000) — the size of the forest. The trees were planted in all points of the (x, y) (1 ≤ x ≤ N, 1 ≤ y ≤ M) type, x and y are integers.
The second line contains an integer K (1 ≤ K ≤ 10) — amount of trees, burning in the beginning.
The third line contains K pairs of integers: x1, y1, x2, y2, …, xk, yk (1 ≤ xi ≤ N, 1 ≤ yi ≤ M) — coordinates of the points from which the fire started. It is guaranteed that no two points coincide.
Output
Output a line with two space-separated integers x and y — coordinates of the tree that will be the last one to start burning. If there are several such trees, output any.
Sample Input
Input
3 3
1
2 2
Output
1 1
Input
3 3
1
1 1
Output
3 3
Input
3 3
2
1 1 3 3
Output
2 2
wa了很多遍~
应当用文件读写!
原因每次搜索到这里就应当比较原有的值与现在的值的大小关系,如果当前值小于原有值就应当更新!
input
input.txt
output
output.txt
After a terrifying forest fire in Berland a forest rebirth program was carried out. Due to it N rows with M trees each were planted and the rows were so neat that one could map it on a system of coordinates so that the j-th tree in the i-th row would have the coordinates of (i, j). However a terrible thing happened and the young forest caught fire. Now we must find the coordinates of the tree that will catch fire last to plan evacuation.
The burning began in K points simultaneously, which means that initially K trees started to burn. Every minute the fire gets from the burning trees to the ones that aren’t burning and that the distance from them to the nearest burning tree equals to 1.
Find the tree that will be the last to start burning. If there are several such trees, output any.
Input
The first input line contains two integers N, M (1 ≤ N, M ≤ 2000) — the size of the forest. The trees were planted in all points of the (x, y) (1 ≤ x ≤ N, 1 ≤ y ≤ M) type, x and y are integers.
The second line contains an integer K (1 ≤ K ≤ 10) — amount of trees, burning in the beginning.
The third line contains K pairs of integers: x1, y1, x2, y2, …, xk, yk (1 ≤ xi ≤ N, 1 ≤ yi ≤ M) — coordinates of the points from which the fire started. It is guaranteed that no two points coincide.
Output
Output a line with two space-separated integers x and y — coordinates of the tree that will be the last one to start burning. If there are several such trees, output any.
Sample Input
Input
3 3
1
2 2
Output
1 1
Input
3 3
1
1 1
Output
3 3
Input
3 3
2
1 1 3 3
Output
2 2
wa了很多遍~
应当用文件读写!
原因每次搜索到这里就应当比较原有的值与现在的值的大小关系,如果当前值小于原有值就应当更新!
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #define inf 0x3f3f3f3f int c[4][2]= {{0,1},{1,0},{-1,0},{0,-1}}; using namespace std; int mmap[2009][2009]; int m,n; int k; int tx,ty; struct node { int x,y; node(int a=0,int b=0):x(a),y(b) {} }; node my,cur,net; int main() { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); scanf("%d%d%d",&n,&m,&k); int x,y; queue<node>que; memset(mmap,inf,sizeof mmap); for(int i=0; i<k; i++) { scanf("%d%d",&x,&y); que.push(node(x,y)); mmap[x][y]=0; } while(!que.empty()) { cur=que.front(); que.pop(); tx=cur.x; ty=cur.y; for(int i=0; i<4; i++) { int xx=cur.x+c[i][0]; int yy=cur.y+c[i][1]; if(xx<1||xx>n||yy<1||yy>m) continue; if(mmap[xx][yy]>mmap[tx][ty]+1) { mmap[xx][yy]=mmap[tx][ty]+1; que.push(node(xx,yy)); } } } int Max=-1,x1,y1; for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) if(mmap[i][j]>Max) { Max=mmap[i][j]; x1=i,y1=j; } // cout<<"--------"<<Max<<endl; printf("%d %d",x1,y1); return 0; }
相关文章推荐
- jQuery on()给动态元素绑定事件
- 慢查询
- PHP+Ajax验证码验证用户登录
- JS获取GET参数
- maven工程常用命令
- Spring中Quartz的配置
- 线段树的代码模板(补充中)
- paxos分布式一致性算法--讲述诸葛亮的反穿越
- 使用Intellij IDEA的svn时提示出错: Can't use Subversion command line client: svn.
- mysql 线程等待时间,解决sleep进程过多的办法 【转】
- 58. Length of Last Word
- eclipse查看jar包源代码乱码问题解决
- <C++略识>之对象数组与对象成员
- ios 充值测试
- Codeforces-698A Vacations
- [置顶] 二维码的生成
- 浅谈LCA的离线算法
- Python 黑帽编程大纲(变化中)
- js 插件 弹出deom 自身属性值
- 博弈论