FZU 1603 Checkers II
来源:互联网 发布:建筑贴膜淘宝 编辑:程序博客网 时间:2024/06/05 04:26
Oaiei’s new problem is showed as follows :
- The chessboard is a straight line which has N lattices.
- There are two kinds of chesses and one empty lattice in the chessboard.
- The initial state will be given arbitrarily.
- There are two possible ways of moving :
- a chess can move directly to the adjacent empty lattice
- a chess can jump over one lattice into the next empty lattice
- The end state will be given arbitrarily too.
- The state is in the following format:
- @ represents the first kind of chesses
- # represents the second kind of chesses
- & represents the empty lattice
- @ and # have the same number
- When you change the initial state into the end state, the game is over.
Input
Output
Sample Input
1
##&@@ @@&##
Sample Output
8
Hint
The one of solutions in the example with the minimum move steps is shown as follows:
represents ‘#’
represents ‘@’
represents ‘&’
题目意思很简单就是问从初状态到末状态要几步,赤裸裸的BFS换来的只能是TLE,当然数据太弱了可能稍微优化下就可以过了,不过只要数据稍微加强一些,那么暴力BFS显然不是一个好的策略;
由于题目的特点,赤裸裸的提示我们使用2进制保存状态,运算速度大大提高,提高为原来的至少N倍左右.
偶的:
161(10)2008-12-12 20:47:10187 ms10204 KC++2133BAekdyCoin
由于棋子只有2种,'#','@',很容易就把'@'看为0,'#'看为'1',那么除了空格'&'以外可以得到一个长度不超过(19-1)=18的2进制数,算出来最多也就(1<<18),而且肯定达不到这么大(1和0的数目相同,最大就是111...000(就是1和0相同个数的状态)),而'&'只与1个,于是就可以开个HASH[(1<<18)][19]
第一维保存的是hash状态值,第二维表示的是'&'后面的有效棋子的个数(0..len-1,len表示输入的字符串的长度).
下面问题就是状态转化的时候需要特别注意:
1.把'&'作为参考点,于是题目所谓的'can move directly to the adjacent empty lattice ',对于这个,很容易发现其hash状态值是不变的,仅'&'的位置发生了变化,就是说'&'后面的棋子的个数变化了.
于是简简单单就处理完了第一种方式.
2.把'&'作为参考点,注意实际上这个操作可以看成2进制里面的2位数的交换,处理使用位运算便可。
参考代码:
- #include<iostream>
- #include<stack>
- using namespace std;
- #define inhash(n) (hash[n.val][n.pos])
- #define showans(a) (printf("%d/n",a))
- bool hash[1<<18][19];
- struct Node
- {
- int val;
- short step,pos;
- bool operator==(const Node&n)const{return val==n.val&&pos==n.pos;}
- }q[950000],tmp,F,D;
- int f,r,tlen,ans,bin[21]={1},low_a,low_b;
- char A[21],B[21];
- inline Node in(char *str)
- {
- int i,key=0;
- tmp.step=0;
- for(i=0;str[i];i++)if(str[i]!='&')key=key*2+(str[i]!='@');else tmp.pos=tlen-i-1;
- tmp.val=key;
- return tmp;
- }
- inline int lowbit(int val,int pos){return (val&bin[pos])!=0;}
- inline bool init()
- {
- memset(hash,false,sizeof(hash));
- f=r=ans=0;
- scanf("%s%s",A,B);
- if(strcmp(A,B)==0)return false;
- tlen=strlen(A);
- D=in(B);
- return true;
- }
- inline void solve()
- {
- F=in(A);
- q[r++]=F;
- hash[F.val][F.pos]=true;
- while(f<r)
- {
- tmp=q[f++];
- tmp.step++;
- if(tmp.pos+2<tlen)
- {
- F=tmp;
- low_a=lowbit(F.val,F.pos+1);
- low_b=lowbit(F.val,F.pos);
- if(low_a!=low_b)F.val+=low_a*bin[F.pos]+low_b*bin[F.pos+1]-low_a*bin[F.pos+1]-low_b*bin[F.pos];
- F.pos+=2;
- if(!inhash(F))
- {
- if(F==D) {ans=F.step;return;}
- hash[F.val][F.pos]=true;
- q[r++]=F;
- }
- }
- if(tmp.pos-2>=0)
- {
- F=tmp;
- low_a=lowbit(F.val,F.pos-2);
- low_b=lowbit(F.val,F.pos-1);
- if(low_a!=low_b)F.val+=low_a*bin[F.pos-1]+low_b*bin[F.pos-2]-low_a*bin[F.pos-2]-low_b*bin[F.pos-1];
- F.pos-=2;
- if(!inhash(F))
- {
- if(F==D) {ans=F.step;return;}
- hash[F.val][F.pos]=true;
- q[r++]=F;
- }
- }
- if(tmp.pos+1<tlen)
- {
- F=tmp;
- F.pos++;
- if(!inhash(F))
- {
- if(F==D) {ans=F.step;return;}
- hash[F.val][F.pos]=true;
- q[r++]=F;
- }
- }
- if(tmp.pos-1>=0)
- {
- F=tmp;
- F.pos--;
- if(!inhash(F))
- {
- if(F==D) {ans=F.step;return;}
- hash[F.val][F.pos]=true;
- q[r++]=F;
- }
- }
- }
- }
- int main()
- {
- int T,i;
- for(i=1;i<=20;i++)bin[i]=bin[i-1]*2;
- scanf("%d%*c",&T);
- while(T--)
- {
- if(init())solve();else ans=0;
- showans(ans);
- }
- return 0;
- }
- FZU 1603 Checkers II
- fzu 1901 Period II
- FZU 1901 Period II
- FZU 1901 Period II
- FZU 1901 Period II
- FZU 1901Period II
- FZU 1888三角形问题II
- fzu 1901 Period II(KMP)
- FZU Problem 1901 Period II
- FZU 2171 防守阵地 II
- FZU 1852 Impossible Mission II
- FZU 1901 Period II (KMP)
- 【hdu3830】Checkers
- fzu 1901 Period II (KMP)
- 【FZU】2171 防守阵地 II 线段树
- FZU 2171 防守阵地 II 线段树
- FZU 题目1901 Period II (KMP)
- FZU 1901 Period II (kmp)
- HttpService
- 使用Request.ServerVariables获取环境变量
- DataTable 类
- XP 允许多用户远程连接
- Rsync服务器端安装配置文档
- FZU 1603 Checkers II
- 配置Java环境变量
- VS2005快捷键复位
- CamShift算法----OpenCV实现1 Back Projection
- 自己动手写一个C++ STL vector
- 两种情况的splitter
- CamShift算法----OpenCV实现2 Mean Shift算法
- 要编写一个程序,控制串口通讯,java里面有...
- J2ME 内存占用的问题