搜索 HOJ 1749 Doggone Moles
2014-02-08 09:53
423 查看
Doggone Moles
My Tags | (Edit) |
---|
Source : ACM ICPC Mid-Atlantic Regional Contest 2004 | |||
Time limit : 1 sec | Memory limit : 32 M |
<W> <L> <N> <T>
W and L are positive integers representing the width (x dimension) and length (y dimension) of the yard. N is the non-negative number of terriers. T is the positive number of time intervals over which we have conducted observations.
The remainder of the observation set contains one line per terrier. Each line contains 2T integers denoting the (x,y) coordinates of the terrier at each of the T time steps, expressed separated by whitespace without parentheses or commas. Possible coordinates range from (0, 0) in one corner of the yard to (W, L) at the opposite corner.
The end of input is signaled by a line containing 4 zeros in place of a valid (W, L, N, T) set.Output[/b]For each observation set, your program should print a line ”Observation Set ” followed by the integer number of the set (starting at 1).If there is at least one possible location for the mole, then, beginning on the next line, print all the possible locations of the mole as (x, y) pairs, 8 pairs per output line (except possibly fewer for the final line of output for the set). There should be no leading blanks before the first pair on a line nor trailing blanks after the final pair on the line, but successive pairs on the same line should be separated by exactly one blank. A pair is printed in the format “(x, y)” with no internal blanks. Pairs should be printed in an order such that pairs with lower values of y come before any pairs with higher values of y and, for pairs with the same y value, pairs with lower values of x come before pairs with higher values of x.If there are no possible locations for the mole, then the second line of output for the observation set will consist of the message “No possible locations”.Sample Input[/b]1 4 2 3
1 1 1 2 1 3
0 1 0 2 0 3
6 2 2 4
3 0 3 1 4 1 4 0
3 1 3 1 4 1 3 1
0 0 0 0This input set may be visualized as:
![](http://acm.hit.edu.cn/hoj/static/img/pic/1749_a.gif)
Sample Output[/b]Observation Set 1
No possible locations
Observation Set 2
(0,0) (1,0) (2,0) (6,0) (0,1) (1,1) (5,1) (6,1)
(0,2) (1,2) (2,2) (4,2) (5,2) (6,2)
题意:有一个农场要捉mole,如果terriers在他旁边或者在它的位置,那么mole就马上被抓住了。求出处经过T时间,mole可能在的位置。
思路:首先这个题目里面,mole和terrirers不是同时运动的。。。这个坑了我很久,terrier先走,如果mole在旁边,就被抓住了,没被抓住,mole才能走。。。。我们用三维数组表示T时间内能去的地方,然后从T=0开始一个一个位置进行dfs,把走到最后的位置记录下来就行了。
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<stack> #include<string.h> #include<algorithm> #include<queue> using namespace std; #define mp make_pair int W,L,T,N; int Move[2][4] = { { -1 , 0 , 1 , 0 } , { 0 , 1 , 0 , -1 } }; bool cango[210][210][210]; bool vis[210][210][210]; struct Ans { Ans(int xx = -1 , int yy = -1) : x (xx) , y (yy) { } int x , y; }ans[110*110]; int top; bool cmp(const Ans & a1 , const Ans & a2) { if (a1.y==a2.y) return a1.x > a2.x; return a1.y > a2.y; } inline bool inRange(int r,int c) { if (r < 0 || r > L || c < 0 || c > W) return false; return true; } void init() { memset(cango,0x3f,sizeof(cango)); memset(vis,0,sizeof(vis)); top = 0; } void input() { for (int i = 0 ; i < N ; ++i) { int r , c; for (int j = 0 ; j < T ; ++j) { scanf("%d%d",&c,&r); cango[j][r][c] = false; for (int k = 0 ; k < 4 ; ++k) { int rr = r+Move[0][k]; int cc = c+Move[1][k]; if (!inRange(rr,cc)) continue; cango[j][rr][cc] = false; } } } } void dfs(int t,int r,int c) { if (!cango[t+1][r][c] || vis[t][r][c]) return; if (t==T-1) { vis[t][r][c] = true; ans[top++] = Ans(c,r); return; } if (cango[t+1][r][c] && !vis[t+1][r][c]) dfs(t+1,r,c); for (int i = 0 ; i < 4 ; ++i) { int rr = r + Move[0][i]; int cc = c + Move[1][i]; if (!inRange(rr,cc) || !cango[t+1][rr][cc] || vis[t+1][rr][cc]) continue; dfs(t+1,rr,cc); } vis[t][r][c] = true; } void solve() { for (int r = 0 ; r <= L ; ++r) { for (int c = 0 ; c <= W ; ++c) if (cango[0][r][c]) { dfs(0,r,c); } } sort(ans,ans+top,cmp); int cnt = 0; if (top) { Ans tmp = Ans(-1,-1); while (top) { tmp = ans[--top]; if (cnt) printf(" "); printf("(%d,%d)",tmp.x,tmp.y); ++cnt; if (cnt==8) { printf("\n"); cnt = 0; } } } else printf("No possible locations\n"); if (cnt) cout << endl; } int main() { int Cas = 0; while (scanf("%d%d%d%d",&W,&L,&N,&T) , N+T+W+L) { ++Cas; init(); input(); printf("Observation Set %d\n",Cas); solve(); } }
相关文章推荐
- 搜索 HOJ 1749 Doggone Moles
- 搜索 HOJ 1114 Frame Stacking
- 搜索 HOJ 1394 Shortcut
- (简单)搜索 HOJ 1692 Choose Your Own Adventure
- 搜索(DLX):HOJ 1017 - Exact cover
- 【搜索剪枝】HOJ 1239 Calling Extraterrestrial Intelligence Again
- 搜索 HOJ 1868 八数码
- Go hdu 4158 hoj 简单搜索
- 搜索 HOJ 1357 Calling Extraterrestrial Intelligence Again
- 搜索(水) HOJ 1870 Race
- 搜索(bfs) HOJ 1448 Dungeon Master
- (简单)搜索 HOJ 1022 Spacecraft Malfunction
- 搜索 HOJ 1320 Square Destroyer
- (简单)搜索 HOJ 1030 Labyrinth
- 搜索 HOJ 1281 Lagrange's Four-Square Theorem
- (简单) 搜索 HOJ 1044 Transportation
- 搜索 HOJ 1273 Peg Game
- (简单) 搜索 HOJ 1048 Cipher
- 搜索(IDA*)HOJ The Rotation Game
- 搜索 HOJ 1226 Lord of the Ring