JOJ 2687
2010-09-24 15:29
190 查看
JOJ
2687
刘备的连营
话说关羽败走麦城,张飞急兄仇反被害后,刘备在夷陵连营七百余里以战孙权。但当马
良将先主的布阵图交与诸葛亮后,孔明大怒,只呼喊:“汉室亡已”。于是连忙让马良劝说先
主赶快改换阵势,否则大事去矣。
现在蜀军已经摆好了如图的阵势,问要变成诸葛孔明所讲的阵势,最少要经过多少次“特
殊变换”。
张苞
关
索
刘备
关兴
傅彤
赵云
程畿
沙摩柯
黄权
原阵势
“特殊变换”:阵势中只有刘备的主营能和它上下左右的营地交换次序。
Input
There are several test cases in the input.
The first line of the input is a number N, standing for the number of the test
cases. Then there will be N block(s), each block has two battle array
representing the starting and ending status of this case. The status is composed
by 9 characters, A-I, each character appears only one time in each status. A
stands for 刘备in the board. The 9 characters are arranged on three lines, each
line has three characters. Each character is separated by one space. There is
a blank line between two statuses and blocks.
Output
Print the string " Battle array #ID :" first, where ID is a positive number, start
from 1. Then on the next line, print "The minimum move of the battle array is :
Times", where Times is the answer you get.
There is a blank line after each battle array.
Sample
Input
2
黄权
张苞
关索
傅彤
关兴
程畿
沙摩柯
赵云
刘备
改变后的
阵势
C I D
B G E
H A F
B C D
I A E
H G F
D E F
B C G
H A I
D E F
B C G
H A I
Sample
Output
Battle array #1 :
The minimum move of the battle array is : 5
Battle array #2 :
The minimum move of the battle array is : 0
Problem Source:allang
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int fac[]= {1,1,2,6,24,120,720,5040,40320};//n!
int hash[400000];
int dir[]={-1,-3,1,3};
bool move[][4] = {0,0,1,1, 1,0,1,1, 1,0,0,1, 0,1,1,1, 1,1,1,1, 1,1,0,1, 0,1,1,0, 1,1,1,0, 1,1,0,0};
struct ss
{
int step;
char x[10];
};
queue<ss> t;
int getkey(char *seq)
{
int i,j,cnt,key=0;
for(i=0; i<9; i++)
{
cnt=0;
for(j=0; j<i; j++)
if(seq[j]>seq[i]) cnt++;
key+=cnt*fac[i]; // cnt<=i
}
return key;
}
int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--)
{
while(!t.empty()) t.pop();
ss m;
char x[15],y[15],dd[15];
for(int i=0;i<9;i++)
{
scanf(" %s",dd);
x[i]=dd[0];
}
x[9]=0;
for(int i=0;i<9;i++)
{
scanf(" %s",dd);
y[i]=dd[0];
}
y[9]=0;
memset(hash,0,sizeof(hash));
int head=0,end=1;
int key=getkey(x);
hash[key]=1;
m.step=0;
strcpy(m.x,x);
t.push(m);
int z,zz;
while(!t.empty())
{
m=t.front();
if(strcmp(m.x,y)==0) break;
for(int i=0; i<9; i++)
if(m.x[i]=='A')
{
z=i;
break;
}
for(int i=0; i<4; i++)
{
strcpy(x,m.x);
if(move[z][i]==0) continue;
int zz=z+dir[i];
char temp=x[z];
x[z]=x[zz];
x[zz]=temp;
int p=getkey(x);
if(hash[p]==0)
{
ss mm;
end++;
hash[p]=1;
mm.step=m.step+1;
strcpy(mm.x,x);
t.push(mm);
}
}
t.pop();
}
printf("Battle array #%d :/n",cas++);
printf("The minimum move of the battle array is : %d/n",m.step);
printf("/n");
}
return 0;
}
2687
刘备的连营
话说关羽败走麦城,张飞急兄仇反被害后,刘备在夷陵连营七百余里以战孙权。但当马
良将先主的布阵图交与诸葛亮后,孔明大怒,只呼喊:“汉室亡已”。于是连忙让马良劝说先
主赶快改换阵势,否则大事去矣。
现在蜀军已经摆好了如图的阵势,问要变成诸葛孔明所讲的阵势,最少要经过多少次“特
殊变换”。
张苞
关
索
刘备
关兴
傅彤
赵云
程畿
沙摩柯
黄权
原阵势
“特殊变换”:阵势中只有刘备的主营能和它上下左右的营地交换次序。
Input
There are several test cases in the input.
The first line of the input is a number N, standing for the number of the test
cases. Then there will be N block(s), each block has two battle array
representing the starting and ending status of this case. The status is composed
by 9 characters, A-I, each character appears only one time in each status. A
stands for 刘备in the board. The 9 characters are arranged on three lines, each
line has three characters. Each character is separated by one space. There is
a blank line between two statuses and blocks.
Output
Print the string " Battle array #ID :" first, where ID is a positive number, start
from 1. Then on the next line, print "The minimum move of the battle array is :
Times", where Times is the answer you get.
There is a blank line after each battle array.
Sample
Input
2
黄权
张苞
关索
傅彤
关兴
程畿
沙摩柯
赵云
刘备
改变后的
阵势
C I D
B G E
H A F
B C D
I A E
H G F
D E F
B C G
H A I
D E F
B C G
H A I
Sample
Output
Battle array #1 :
The minimum move of the battle array is : 5
Battle array #2 :
The minimum move of the battle array is : 0
Problem Source:allang
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int fac[]= {1,1,2,6,24,120,720,5040,40320};//n!
int hash[400000];
int dir[]={-1,-3,1,3};
bool move[][4] = {0,0,1,1, 1,0,1,1, 1,0,0,1, 0,1,1,1, 1,1,1,1, 1,1,0,1, 0,1,1,0, 1,1,1,0, 1,1,0,0};
struct ss
{
int step;
char x[10];
};
queue<ss> t;
int getkey(char *seq)
{
int i,j,cnt,key=0;
for(i=0; i<9; i++)
{
cnt=0;
for(j=0; j<i; j++)
if(seq[j]>seq[i]) cnt++;
key+=cnt*fac[i]; // cnt<=i
}
return key;
}
int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--)
{
while(!t.empty()) t.pop();
ss m;
char x[15],y[15],dd[15];
for(int i=0;i<9;i++)
{
scanf(" %s",dd);
x[i]=dd[0];
}
x[9]=0;
for(int i=0;i<9;i++)
{
scanf(" %s",dd);
y[i]=dd[0];
}
y[9]=0;
memset(hash,0,sizeof(hash));
int head=0,end=1;
int key=getkey(x);
hash[key]=1;
m.step=0;
strcpy(m.x,x);
t.push(m);
int z,zz;
while(!t.empty())
{
m=t.front();
if(strcmp(m.x,y)==0) break;
for(int i=0; i<9; i++)
if(m.x[i]=='A')
{
z=i;
break;
}
for(int i=0; i<4; i++)
{
strcpy(x,m.x);
if(move[z][i]==0) continue;
int zz=z+dir[i];
char temp=x[z];
x[z]=x[zz];
x[zz]=temp;
int p=getkey(x);
if(hash[p]==0)
{
ss mm;
end++;
hash[p]=1;
mm.step=m.step+1;
strcpy(mm.x,x);
t.push(mm);
}
}
t.pop();
}
printf("Battle array #%d :/n",cas++);
printf("The minimum move of the battle array is : %d/n",m.step);
printf("/n");
}
return 0;
}
相关文章推荐
- joj2687
- joj 儿死三八
- JOJ 1903 1129 1157 1278
- JOJ 1065: Truck Driver 解题报告
- joj 1026: The Staircases
- joj 1020 格式输出
- joj2697
- joj 2699: 16 and 18 博弈 最后一个取石子者输
- joj 1173
- joj 2736: 二叉树计数 卡特兰数取模 1<=n<=10^9,1<=m<=32768
- joj 1509Hamiltonian Cycle (dfs求H回路)
- joj 2426
- joj1023
- joj2170
- JOJ 2474:Tile My Corridor 平铺棋盘 状态压缩解法
- 区间素数筛选法 joj 1928 Prime Distance
- joj 2699
- joj1131
- joj1966
- JOJ 2453 Candy