eoj1120 dfs

Peg Game

Time Limit:1000MSMemory Limit:30000KB
Total Submit:254Accepted:97 Special 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 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.

方品  10121540117

题目:EOJ1120

 

题目分析:将地图保存在map[][]数组中,E或O的地方为最终的目的地,记录此点坐标。用dx[]和dy[]表示方向。为避免讨论是否跳出地图范围内,可在地图的上下左右各加两排x。用dfs搜索。一次枚举每一个位置,若此处为o并且可以”跳”,则保存当前路径,更改当前地图,往下一层dfs,若无法继续跳则回溯,恢复上一层地图原状,继续dfs。

 

 

AC代码:

#include <iostream>

#include <cstring>

#include <cstdio>

 

using namespace std;

struct Ans{

   int x,y,a,b;

}ans[49];

bool success=false;

char map[13][13];

int dx[]={-1,1,0,0},dy[]={0,0,-1,1};

 

void dfs(int deep,int x,int y,int cnt){

   if(success) return;

   if(deep==1){

       if(map[x][y]=='o'){

           int ca=1;

           for(;cnt>1;--cnt){

                cout<<ca++<<".("<<ans[cnt].y<<" ,"

               <<ans[cnt].x<<") to("<<ans[cnt].b<<", "<<ans[cnt].a

               <<")"<<endl;

           }

           cout<<endl;

           success=true;

           return ;

       }

       return;

    }

   int i,j,k;

   for(i=2;i<9;++i){

       for(j=2;j<9;++j){

           if(map[i][j]=='o'){

                for(k=0;k<4;++k){

                    int xx=i+dx[k],yy=j+dy[k];

                   if(map[xx][yy]=='o'){

                        intxxx=xx+dx[k],yyy=yy+dy[k];

                        if(map[xxx][yyy]=='e'){

                            map[i][j]='e';

                            map[xx][yy]='e';

                            map[xxx][yyy]='o';

                            ans[deep].x=i-1;

                            ans[deep].y=j-1;

                            ans[deep].a=xxx-1;

                            ans[deep].b=yyy-1;

                           dfs(deep-1,x,y,cnt);

                           map[i][j]='o';

                            map[xx][yy]='o';

                            map[xxx][yyy]='e';

                        }

                    }

                }

           }

       }

    }

}

 

int main()

{

   int t,i,j,x,y;

   char tem[5];

   scanf("%d",&t);

   memset(map,'x',sizeof(map));

   for(int ii=0;ii<t;++ii){

       int cnt=0;

       for(i=2;i<9;++i){

           for(j=2;j<9;++j){

                scanf("%s",tem);

                map[i][j]=tem[0];

                {

                    if(map[i][j]=='E' ||map[i][j]=='O'){

                        x=i;y=j;

                    }

                    if(map[i][j]=='E')

                        map[i][j]='e';

                    if(map[i][j]=='O')

                        map[i][j]='o';

                    if(map[i][j]=='o')

                        ++cnt;

                }

           }

       }

       cout<<"Dataset"<<ii+1<<":"<<endl;

       dfs(cnt,x,y,cnt);

       if(!success)

           printf("No solution.\n\n");

    }

   return 0;

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值