搜索 HOJ 1273 Peg Game
来源:互联网 发布:下载软件游戏中心 编辑:程序博客网 时间:2024/05/02 22:04
Peg Game
Submitted : 73, Accepted : 31
You are given a 7-by-7 board of holes. Some holes are filled with pegs, and some are not. You may jump a peg over an adjacent peg, as long as the hole the jumping peg lands in is unoccupied. The jumped peg is removed. Your goal is to leave the board with only one peg in it, and the peg must end up in the specified location.
The board is specified as a 7-by-7 array of characters, with the following meanings:
x: this hole may never be occupied by a peg
e: this hole is initially empty
o: this hole is initially occupied by a peg
E: this hole is initially empty, and the last peg should end here
O: this hole is initially occupied, and the last peg should end here
For example, consider the following board:
x x e e e x x
x x o e e x x
e e o e e e e
e e o O e e e
e e e e e e e
x x e e e x x
x x e e e x x
You can see that there are initially 4 pegs in the board, and the last peg should end up in the middle of the board. The winning sequence of moves is:
1. (4, 4) to (2, 4)
2. (3, 2) to (3, 4)
3. (2, 4) to (4, 4)
Where coordinates are given as (x, y).
Input
The first line of input is the number of datasets to follow. Each dataset should be processed the same.
The input for each dataset consists of 7 lines; each line consists of 7 characters from the set {x, e, o, E, O} with blanks between them. You are guaranteed that exactly one 'E' or 'O' will appear, and that two or more 'o' or 'O' will appear.
Output
For each dataset, output a line containing the data set number. If a sequence of valid moves exists that leaves only one peg on the board, and leaves that peg in the desired location, print out the sequence of moves, as shown in the above example. If no sequence exists, print "No solution". Leave a blank line between datasets.
Sample Input
2x x e e e x xx x o e e x xe e o e e e ee e o O e e ee e e e e e ex x e e e x xx x e e e x xx x e E e e ex e e e e e ee e e o o e ee e e x e e ee e e e e e ee e e e e e ee e e e e e e
Sample Output
Dataset 1:1. (4, 4) to (2, 4)2. (3, 2) to (3, 4)3. (2, 4) to (4, 4)Dataset 2:No solution.
#include<iostream>#include<algorithm>#include<vector>#include<math.h>#include<cstdio>#include<string.h>#include<cstring>using namespace std;#define mp make_pairchar G[15][15];char buffer[4];int Move[2][4] = { {0,1,0,-1} , {1,0,-1,0} };int dest_r , dest_c , cnt , Cas = 0;typedef pair<int,int> node;node stk[1000];int top;void init(){top = cnt = 0;for (int i = 0 ; i < 15 ; ++i)for (int j = 0 ; j < 15 ; ++j) G[i][j] = 'x';}void input(){for (int i = 2 ; i < 2+7 ; ++i){for (int j = 2 ; j < 9 ; ++j){scanf("%s",buffer);G[i][j] = buffer[0];}}for (int i = 2 ; i < 9 ; ++i) {for (int j = 2 ; j < 9 ; ++j){if (G[i][j]=='E' || G[i][j]=='O')dest_r = i , dest_c = j;if (G[i][j]=='o' || G[i][j]=='O') ++cnt , G[i][j] = 'o';if (G[i][j]=='E') G[i][j] = 'e';}}}bool dfs(int rest){if (rest==1){if (G[dest_r][dest_c]=='o') return true;return false;}for (int i = 2 ; i < 9 ; ++i){for (int j = 2 ; j < 9 ; ++j) if (G[i][j]=='o'){for (int k = 0 ; k < 4 ; ++k){int r = i+Move[0][k];int c = j+Move[1][k];if (G[r][c]!='o') continue;int rr = r+Move[0][k];int cc = c+Move[1][k];if (G[rr][cc]!='e') continue;G[i][j] = 'e';G[r][c] = 'e';G[rr][cc] = 'o';if (dfs(rest-1)){stk[top++] = mp(rr-1,cc-1);stk[top++] = mp(i-1,j-1);return true;}G[rr][cc] = 'e';G[r][c] = 'o';G[i][j] = 'o';}}}return false;}void solve(){if (!dfs(cnt))printf("No solution.\n");else {int step = 1;for (int i = top-1 ; i >= 1 ; i -= 2)printf("%d. (%d, %d) to (%d, %d)\n",step,stk[i].second,stk[i].first,stk[i-1].second,stk[i-1].first);}}int main(){int T;cin>>T;while (T--){++Cas;printf("Dataset %d:\n",Cas);init();input();solve();if (T) printf("\n");}}
- 搜索 HOJ 1273 Peg Game
- 搜索 HOJ 1140 The Game
- 搜索(IDA*)HOJ The Rotation Game
- EOJ 1120 Peg Game
- Sicily 1053. Peg Game
- 搜索 HOJ 1736 A Game on the Chessboard
- HOJ 2785 The Game
- Hoj 1140 The Game
- 搜索 HOJ 1629 Safecracker
- 搜索 HOJ 1394 Shortcut
- 搜索 HOJ 1136 Robbery
- HOJ 1846 博弈 BASH game
- 博弈论 HOJ 1122 Number Game
- 搜索 HOJ 1838 Traffic Jam
- 搜索 HOJ 1749 Doggone Moles
- 搜索 HOJ 1193 Anagram checker
- 搜索 HOJ 1114 Frame Stacking
- 搜索 HOJ 1868 八数码
- 搜索(bfs) HOJ 1448 Dungeon Master
- 搜索 HOJ 1320 Square Destroyer
- 通过Zimbra收取POP3邮件,总是提示错误:Connection reset
- 搜索 HOJ 1281 Lagrange's Four-Square Theorem
- C++判断三角形
- 搜索 HOJ 1273 Peg Game
- 『黑马程序员』---java--IO--Properties-打印流-序列流基础
- CentOS 5.8 Zimbra邮件系统安装与配置
- 搜索 HOJ 1226 Lord of the Ring
- Retrieving a List of Contacts
- 搜索 HOJ 1140 The Game
- 搜索 HOJ 1136 Robbery
- 第一章 HTTP消息机制和经典的同步I/O流
- 搜索 HOJ 1126 Smith Numbers