Fire Again
2015-11-15 10:34
941 查看
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
题意:给你n X m 的坐标,再给你k个着火点,求最后一个着火的位置。
下面给你两种方法解释一下,暴力和广搜。
1.暴力 :
2.广搜:
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
题意:给你n X m 的坐标,再给你k个着火点,求最后一个着火的位置。
下面给你两种方法解释一下,暴力和广搜。
1.暴力 :
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> #include <math.h> using namespace std; int time[2001][2001], b[2001][2001], n, m; int abs(int x) { if(x < 0) return -x; return x; } void init() { for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) b[i][j] = 4001; } } int main() { int k, x, y, maxx, minn; freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); while(~scanf("%d %d", &n, &m)) { maxx = -1; init(); memset(time, 0, sizeof(time)); scanf("%d", &k); for(int i = 0; i < k; i++) { scanf("%d %d", &x, &y); for(int j = 1; j <= n; j++) { for(int l = 1; l <= m; l++) { int x1 = abs(j - x); int y1 = abs(l - y); time[j][l] = (x1 + y1); if(b[j][l] > time[j][l]) b[j][l] = time[j][l]; } } } int t1 = 0, t2 = 0; for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { if(maxx <= b[i][j]) { maxx = b[i][j]; t1 = i; t2 = j; } } } printf("%d %d\n", t1, t2); } return 0; }
2.广搜:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> #include <queue> using namespace std; int vis[2001][2001], n, m; int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1}; struct cc { int x, y; }vv, res; int check(int x, int y) { if(x >= 1 && x <= n && y >= 1 && y <= m) return 1; return 0; } queue <cc> qq; void bfs() { while(!qq.empty()) { vv = qq.front(); qq.pop(); int x0 = vv.x; int y0 = vv.y; res = vv; for(int i = 0; i < 4; i++) { int x1 = x0 + dir[i][0]; int y1 = y0 + dir[i][1]; if(check(x1, y1) && !vis[x1][y1]) { vis[x1][y1] = 1; vv.x = x1; vv.y = y1; res = vv; qq.push(vv); } } } } int main() { int k, a, b; freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); while(~scanf("%d %d", &n, &m)) { memset(vis, 0, sizeof(vis)); scanf("%d", &k); for(int i = 0; i < k; i++) { scanf("%d %d", &a, &b); vv.x = a; vv.y = b; vis[a][b] = 1; qq.push(vv); } bfs(); printf("%d %d\n", res.x, res.y); } return 0; }
相关文章推荐
- 关于RAID的技术知识
- 关于不同的程序入口,main(), _tmain(),WinMain(),wmain()?
- max-min fairness 最大最小公平算法
- LeetCode Contains Duplicate
- Brainiac Designer
- 书-Programming Game AI by Example
- Bad Hair Day(单调栈 )
- AIDL Service
- POJ 3250 Bad Hair Day (栈)
- poj Bad Hair Day 3250 (栈++技巧) 好题
- 在Django块标签中遇到的Error:Could not parse the remainder:'>' from 'user.age>'
- 【索引】2013 Multi-University Training Contest
- 九度OJ 1326:Waiting in Line(排队) (模拟)
- 九度OJ 1326:Waiting in Line(排队) (模拟)
- 219 Contains Duplicate II
- hdu3394Railway【双连通分量+模板详细解释】
- 172 Factorial Trailing Zeroes
- 70 Climbing Stairs
- 217 Contains Duplicate
- POJ 3250 Bad Hair Day(单调递增栈)