2014牡丹江网络预选赛F题(隐式图BFS暴搜)zoj3814

Sawtooth Puzzle

Time Limit: 10 Seconds       Memory Limit: 65536 KB

Recently, you found an interesting game called Sawtooth Puzzle. This is a single-player game played on a grid with 3 x 3 cells. Each cell contains a part of an image. Besides, each edge of a cell will be either smooth or saw-toothed. Two edge-adjacent cells are considered as meshed if their adjacent edges are both saw-toothed.

The game contains zero or more steps. At each step, the player can choose one cell, rotate it by 90 degrees clockwise. At the beginning of rotation, any cell which is going to rotate will drive other cells to rotate together if they are meshed. Of course, the driven (passive) cell will rotate in the opposite direction against the driver (active) cell. The following image demonstrates the procedure of rotating the middle cell clockwise:

Given the initial image and target image of the puzzle, please find out the minimum number of steps needed to solve it.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The input data consists of three parts. The first part represents the initial image of the puzzle. It contains 26 lines. Each line has 26 characters (excluding the line break '\n'). The image is composed of 3 x 3 cells. Each cell is a monochrome image of 8 x 8 pixels ('#' for black, '.' for white). These cells are separated by space characters of 1 pixel width.

The second part represents the target image of the puzzle. It has the same format as which described above.

The last part contains 9 lines. Each line has 4 integers indicating the edge type (0 for smooth, 1 for saw-toothed) of a cell. The cells are listed in the order of top-left, top-middle, top-right, middle-left, ... , till bottom-right. For each cell, the edge type are given in the order of left, top, right and bottom.

There is an empty line between any two adjacent parts (including of different cases).

Output

For each test case, output the minimum number of steps needed to solve the puzzle. If there is no solution, output -1 instead.

Sample Input
1
......#. ........ ........
......## ........ ........
.......# #....... ........
.......# ##...... ...##...
.......# ###..... .###....
.......# ####...# ####....
.......# ######## ###.....
.......# ######## ###.....
                          
........ ######## ###.....
.......# ######## ##......
.....### ######## ##......
....#### ######## ###.....
..###### ######## ####....
######## ######## #####...
...##### ######## ######..
........ ######## #######.
                          
........ ........ ..######
........ .....### ........
........ ######## ........
........ .####### ........
........ ...##### ........
........ .....### ........
........ .......# ........
........ ........ ........

......#. ........ ........
......## ........ ........
.......# #....... ........
.......# ##...... ...##...
.......# ###..... .###....
.......# ####...# ####....
.......# ######## ###.....
.......# ######## ###.....
                          
........ ######## ###.....
.......# ######## ##......
.....### ######## ##......
....#### ######## ###.....
..###### ######## ####....
######## ######## #####...
...##### ######## ######..
........ ######## #######.
                          
........ .######. ..######
........ .#####.. ........
........ .#####.. ........
........ ..###... ........
........ ..###... ........
........ ..##.... ........
........ ..##.... ........
........ ..#..... ........

0 1 0 1
0 0 1 1
1 0 0 1
0 0 1 1
1 1 1 0
1 0 1 1
1 1 1 1
0 0 1 0
0 1 1 0
Sample Output
6

题意:RT

思路:BFS暴搜

            注意以下几点即可:

            1.每次扩展状态的时候要对9个方块分别右旋

            2.终点的状态可能有很多个,因为有的格子旋转以后的图案还是和之前一样

            3.处理的时候注意一下细节即可(这个把我坑了好久,比如我在旋转第3个块的时候把它左边的块也旋转了,实际上它的左边是没有块的)
            
#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         using namespace std; char mp[9][10][10]; char gmp[9][10][10]; int ha[9][4]; int hehe[9][4]; //标记终点的未压缩的状态 (8维) int en[300000]; //标记终点的压缩后的状态 (一个数字) int vis[300000]; //记录步数,顺便标记状态 int q[300000]; //队列,用来扩展整个状态 int qsmall[15]; //队列,用来扩展每个方块的旋转的状态(周围四个) bool fx(int vis) { for(int i=0;i<8;i++) for(int j=0;j<8;j++) if(mp[vis][i][j]!=gmp[vis][i][j])return false; return true; } void clockwise(int vis) { char temp[8][8]; for(int i=0;i<8;i++) for(int j=0;j<8;j++) { temp[i][j]=mp[vis][7-j][i]; } for(int i=0;i<8;i++) for(int j=0;j<8;j++) mp[vis][i][j]=temp[i][j]; } inline int add(int a,int b) { a+=b; if(a>=4)a-=4; return a; } inline int sub(int a,int b) { a-=b; if(a<0)a+=4; return a; } inline bool gx(int x) { if(x<0||x>8)return false; return true; } int temp[9]; int nowha[9][4]; int rot(int root) //旋转 { int front=0,rear=0; bool vs[9],vt[9]; int tem[9]; for(int i=0;i<9;i++)tem[i]=temp[i]; memset(vs,false,sizeof(vs)); memset(vt,false,sizeof(vt)); qsmall[rear++]=root; vs[root]=vt[root]=true; tem[root]=add(temp[root],1); int pos[4]; while(front 
        
          =0;--i) { temp[i]=tf%4; //提取状态 tf/=4; int v=temp[i]; for(int j=0;j<4;++j) //对每个位置的方块处理出它的毛边 nowha[i][j]=ha[i][add(sub(j,v),4)]; } for(int i=0;i<9;++i) //对9个位置的方块进行右旋(走一步),拓展状态 { int ans=rot(i); if(ans!=-1){ //对旋转之后的状态进行判断,如果没有拓展过就进队 vis[ans]=vis[f]+1; //记录步数 if(en[ans])return vis[ans]-1; //走到终点 q[rear++]=ans; } } } return -1; } void dfs(int cent,int v) { if(cent==9) { en[v]=1; return; } for(int i=0;i<4;i++) { if(hehe[cent][i]) { dfs(cent+1,v*4+i); } } } int main() { int t; scanf("%d",&t); while(t--) { for(int i=0;i<24;i++) { int cnt=i/8*3; for(int j=0;j<3;j++) { scanf("%s",mp[cnt+j][i%8]); } } for(int i=0;i<24;i++) { int cnt=i/8*3; for(int j=0;j<3;j++) { scanf("%s",gmp[cnt+j][i%8]); } } for(int i=0;i<9;i++) for(int j=0;j<4;j++)scanf("%d",&ha[i][j]); memset(hehe,0,sizeof(hehe)); memset(en,0,sizeof(en)); for(int i=0;i<9;i++) { for(int k=0;k<4;k++) { bool ok=fx(i); if(ok){ hehe[i][k]=1; } clockwise(i); } } dfs(0,0); //标出所有的终点的状态 int ans=bfs(); //开始走 printf("%d\n",ans); } } 
         
       
      
      
     
     
    
    
   
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值