[BZOJ1085][SCOI2005]骑士精神
2015-07-20 00:09
399 查看
原题地址
A*.
AC code:
A*.
AC code:
#include <cstdio> int n,ans; int mx[9]={0,-2,-1,1,2,2,1,-1,-2},my[9]={0,1,2,2,1,-1,-2,-2,-1}; char s[6][6]; void build(){ s[1][1]=s[1][2]=s[1][3]=s[1][4]=s[1][5]=s[2][2]=s[2][3]=s[2][4]=s[2][5]=s[3][4]=s[3][5]=s[4][5]='1'; s[2][1]=s[3][1]=s[3][2]=s[4][1]=s[4][2]=s[4][3]=s[4][4]=s[5][1]=s[5][2]=s[5][3]=s[5][4]=s[5][5]='0'; } int h(char a[6][6]){ int cnt=0; for(int i=1;i<=5;i++){ for(int j=1;j<=5;j++){ if((i!=3||j!=3)&&a[i][j]!=s[i][j]) cnt++; } } return cnt; } void swap(char *a,char *b){ char t=*a;*a=*b;*b=t; } void DFS(char a[6][6],int step){ if(step+h(a)>=ans) return ; else if(!h(a)) ans=step<ans?step:ans; int x,y; for(int i=1;i<=5;i++){ for(int j=1;j<=5;j++){ if(a[i][j]=='*'){ x=i;y=j; break; } } } for(int i=1;i<=8;i++){ char b[6][6]; int xx=x+mx[i],yy=y+my[i]; if(xx<1||xx>5||yy<1||yy>5) continue; for(int j=1;j<=5;j++){ for(int k=1;k<=5;k++) b[j][k]=a[j][k]; } swap(&b[x][y],&b[xx][yy]); DFS(b,step+1); } } int main(){ build(); scanf("%d\n",&n); while(n--){ char map[6][6]; for(int i=1;i<=5;i++){ for(int j=1;j<=5;j++) scanf("%c",&map[i][j]); scanf("\n"); } ans=16; DFS(map,0); if(ans==16) printf("-1\n"); else printf("%d\n",ans); } return 0; }
相关文章推荐
- Picasso:一个强大的图片下载和缓存库
- C Language Study - gets , getchar & scanf
- 我想做一个合格的工程师
- Exchange Server 2013 公网发布疑难解答
- 猜拳小游戏过程以及感悟
- [BZOJ2456]mode
- Ninject依赖注入——构造函数、属性、方法和字段的注入
- 解决The current branch is not configured for pull No value for key branch.master.merge found in config
- FreeBSD 10添加Swap文件
- eclipse连接hbase异常KeeperErrorCode = ConnectionLoss for /hbase/meta-region-server
- hdu1072(bfs搜索退出的条件有变)
- [BZOJ1798][Ahoi2009]Seq 维护序列seq
- 2015年ALPC暑期专题练习I (计算几何) D Intersecting Lines
- poj1179polygon
- android studio中文乱码各种情况的解决办法
- a new star
- VB.NET调用MySQL存储过程并获得返回值的方法
- ASP.NET Web API如何将注释自动生成帮助文档
- 在Django中创建URLconf相关的通用视图的方法
- Django框架中处理URLconf中特定的URL的方法