Peg Game
Time Limit:1000MSMemory Limit:30000KBTotal Submit:224Accepted:86Special Judge
Description
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
2 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 x x e E e e e x e e e e e e e e e o o e e e e e x e e e e e e e e e e e e e e e e e e e e e e e eSample 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.写个BFS吧,就是长了点。。
#include#include #include #include using namespace std;struct data{ char s[8][8]; int id;};struct point{ int sx,sy,ex,ey;};point p[1000050];int pre[1000050];int ex,ey;int id;int ca = 1;void path(int id){ //printf("id = %d\n",id); if(id)path(pre[id]); if(id)printf("%d. (%d, %d) to (%d, %d)\n",ca ++,p[id].sy + 1,p[id].sx + 1,p[id].ey + 1,p[id].ex + 1);}bool judge(data a){ int cnt = 0; int tx = -1,ty = -1; for(int i = 0;i < 7;i ++){ for(int j = 0;j < 7;j ++){ //printf("%c",a.s[i][j]); if(a.s[i][j] == 'o'){ cnt ++; tx = i; ty = j; } } //printf("\n"); } return cnt == 1 && tx == ex && ty == ey;}bool in(int x,int y){ return x >= 0 && x < 7 && y >= 0 && y < 7;}int main(){ int t; int cc = 1; scanf("%d",&t); getchar(); while(t --){ data tp; char c; for(int i = 0;i < 7;i ++){ for(int j = 0;j < 7;j ++){ //scanf("%c%c",&tp.s[i][j],&c); cin>>tp.s[i][j]; //printf("%c%c",tp.s[i][j],c); if(tp.s[i][j] == 'O'){ ex = i;ey = j; tp.s[i][j] = 'o'; } else if(tp.s[i][j] == 'E'){ ex = i;ey = j; tp.s[i][j] = 'e'; } } } memset(pre,0,sizeof(pre)); id = 0; ca = 1; tp.id = id++; queue q; bool flag = false; q.push(tp); while(!q.empty()){ tp = q.front(); q.pop(); if(judge(tp)){ flag = true; break; } //printf("id = %d\n",tp.id); data pp = tp; for(int i = 0;i < 7;i ++){ for(int j = 0;j < 7;j ++){ if(tp.s[i][j] == 'o'){ if(in(i - 2,j) && tp.s[i - 1][j] == 'o' && tp.s[i - 2][j] == 'e'){ pp.s[i][j] = 'e'; pp.s[i - 1][j] = 'e'; pp.s[i - 2][j] = 'o'; p[id].sx = i; p[id].sy = j; p[id].ex = i - 2; p[id].ey = j; pp.id = id; //printf("%d %d %d %d\n",p[id].sx,p[id].sy,p[id].ex,p[id].ey); q.push(pp); pre[id ++] = tp.id; } pp = tp; if(in(i,j + 2) && tp.s[i][j + 1] == 'o' && tp.s[i][j + 2] == 'e'){ pp.s[i][j] = 'e'; pp.s[i][j + 1] = 'e'; pp.s[i][j + 2] = 'o'; p[id].sx = i; p[id].sy = j; p[id].ex = i; p[id].ey = j + 2; pp.id = id; //printf("%d %d %d %d\n",p[id].sx,p[id].sy,p[id].ex,p[id].ey); q.push(pp); pre[id ++] = tp.id; } pp = tp; if(in(i + 2,j) && tp.s[i + 1][j] == 'o' && tp.s[i + 2][j] == 'e'){ pp.s[i][j] = 'e'; pp.s[i + 1][j] = 'e'; pp.s[i + 2][j] = 'o'; p[id].sx = i; p[id].sy = j; p[id].ex = i + 2; p[id].ey = j; pp.id = id; //printf("%d %d %d %d\n",p[id].sx,p[id].sy,p[id].ex,p[id].ey); q.push(pp); pre[id ++] = tp.id; } pp = tp; if(in(i,j - 2) && tp.s[i][j - 1] == 'o' && tp.s[i][j - 2] == 'e'){ pp.s[i][j] = 'e'; pp.s[i][j - 1] = 'e'; pp.s[i][j - 2] = 'o'; p[id].sx = i; p[id].sy = j; p[id].ex = i; p[id].ey = j - 2; pp.id = id; //printf("%d %d %d %d\n",p[id].sx,p[id].sy,p[id].ex,p[id].ey); q.push(pp); pre[id ++] = tp.id; } } } } } printf("Dataset %d:\n",cc ++); if(flag)path(tp.id); else printf("No solution.\n"); if(t)printf("\n"); } return 0;}