POJ 1198 Solitaire(bfs)
2016-08-27 21:08
225 查看
Solitaire is a game played on a chessboard 8x8. The rows and columns of the chessboard are numbered from 1 to 8, from the top to the bottom and from left to right respectively.
There are four identical pieces on the board. In one move it is allowed to:
move a piece to an empty neighboring field (up, down, left or right),
jump over one neighboring piece to an empty field (up, down, left or right).
There are 4 moves allowed for each piece in the configuration shown above. As an example let's consider a piece placed in the row 4, column 4. It can be moved one row up, two rows down, one column left or two columns right.
Write a program that:
reads two chessboard configurations from the standard input,
verifies whether the second one is reachable from the first one in at most 8 moves,
writes the result to the standard output.
Input
Each of two input lines contains 8 integers a1, a2, ..., a8 separated by single spaces and describes one configuration of pieces on the chessboard. Integers a2j-1 and a2j (1 <= j <= 4) describe the position of one piece -- the
row number and the column number respectively.
Output
The output should contain one word YES if a configuration described in the second input line is reachable from the configuration described in the first input line in at most 8 moves, or one word NO otherwise.
Sample Input
Sample Output
题意是四个棋子,给出起始位置和终止位置(无序),问是否在8步之内满足要求。
我用的是单向bfs
^-^come on!
There are four identical pieces on the board. In one move it is allowed to:
move a piece to an empty neighboring field (up, down, left or right),
jump over one neighboring piece to an empty field (up, down, left or right).
There are 4 moves allowed for each piece in the configuration shown above. As an example let's consider a piece placed in the row 4, column 4. It can be moved one row up, two rows down, one column left or two columns right.
Write a program that:
reads two chessboard configurations from the standard input,
verifies whether the second one is reachable from the first one in at most 8 moves,
writes the result to the standard output.
Input
Each of two input lines contains 8 integers a1, a2, ..., a8 separated by single spaces and describes one configuration of pieces on the chessboard. Integers a2j-1 and a2j (1 <= j <= 4) describe the position of one piece -- the
row number and the column number respectively.
Output
The output should contain one word YES if a configuration described in the second input line is reachable from the configuration described in the first input line in at most 8 moves, or one word NO otherwise.
Sample Input
4 4 4 5 5 4 6 5 2 4 3 3 3 6 4 6
Sample Output
YES
题意是四个棋子,给出起始位置和终止位置(无序),问是否在8步之内满足要求。
我用的是单向bfs
#include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<queue> using namespace std; #define MAXN 9 int fx[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; bool vis[MAXN][MAXN][MAXN][MAXN][MAXN][MAXN][MAXN][MAXN]; bool map[MAXN][MAXN]; struct node{ int x[5], y[5]; int dep; }; node s, t; bool check(node p){ for(int i = 1; i <= 4; i++) if(!map[p.x[i]][p.y[i]]) return false; return true; } bool ok(int X, int Y){ if(X <= 0 || Y <= 0 || X > 8 || Y > 8) return false; return true; } bool ok_c(node t){ bool &tmp = vis[t.x[1]][t.x[2]][t.x[3]][t.x[4]][t.y[1]][t.y[2]][t.y[3]][t.y[4]]; if(tmp) return false; return tmp = true; } bool empty(int X, int Y, node now){ for(int i = 1; i <= 4; i++) if(X == now.x[i] && Y == now.y[i]) return false; return true; } void bfs(){ queue<node> q; q.push(s); ok_c(s); while(!q.empty()){ node now = q.front(); q.pop(); if(now.dep >= 8){ printf("NO\n"); return; } for(int i = 1; i <= 4; i++){ int X = now.x[i], Y = now.y[i]; for(int j = 0; j < 4; j++){ node next = now; next.dep = now.dep+1; int nx = X + fx[j][0], ny = Y + fx[j][1]; if(!ok(nx, ny)) continue; next.x[i] = nx; next.y[i] = ny; if(empty(nx, ny, now)){ next.x[i] = nx; next.y[i] = ny; if(!ok_c(next)) continue; if(check(next)){ printf("YES\n"); return; } q.push(next); }else{ nx += fx[j][0]; ny += fx[j][1]; if(!ok(nx, ny)) continue; next.x[i] = nx; next.y[i] = ny; if(!ok_c(next) || !empty(nx, ny, now)) continue; if(check(next)){ printf("YES\n"); return; } q.push(next); } } } } printf("NO\n"); return; } int main(){ freopen("test.in", "r", stdin); freopen("test.out", "w", stdout); for(int i = 1; i <= 4; i++) scanf("%d%d", &s.x[i], &s.y[i]); for(int i = 1; i <= 4; i++){ scanf("%d%d", &t.x[i], &t.y[i]); map[t.x[i]][t.y[i]] = true; } s.dep = 0; bfs(); return 0; }
^-^come on!
相关文章推荐
- DBFS POJ1198 Solitaire
- AIM Tech Round 3 (Div. 2)
- Contains Duplicate II
- BZOJ4690: Never Wait for Weights
- 构建人工辅助AI应用
- [Google Round A APAC 2017] Problem B. Rain
- RAID
- Spark Worker: Failed to connect to master master:7077
- JBoss AS domain模式配置
- 关于failed to load the JNI 的问题
- 加载web view的时候 Domain=WebKitErrorDomain Code=101 “The operation couldn’t be completed. (WebKitErrorDo
- AIM Tech Round 3 (Div. 2) -- B. Checkpoints (枚举)
- Gym 100646H You’ll be Working on the Railroad(搜索)
- poj 3227 Mountains
- found that when click menu then click again, back to main activity but not the one when click menu.
- Codeforces Round #272 (Div. 2) A Dreamoon and Stairs(水)
- Error:has both ordered [`.ARM.exidx' in main.o] and unordered [`.ARM.extab' in main.o] sections
- LVM和raid结合使用
- AIM Tech Round 3 (Div. 2) E. Connecting Universities 【构造】
- AIM Tech Round 3 (Div. 1) C. Centroids(树形dp)