我要一改我懒惰的作风
这道题目必须放题面
文件中包含初始的盘件描述,一共有10行,第i行有i个字符。
如果第i行的第j个字符是字母”A”至”L”中的一个,则表示第i行第j列的格子上已经放了零件,零件的编号为对应的字母。
如果第i行的第j个字符是”.”,则表示第i行第j列的格子上没有放零件。
输入保证预放的零件已摆放在盘件中。
如果能找到解,向输出文件打印10行,为放完全部12个零件后的布局。
其中,第i行应包含i个字符,第i行的第j个字符表示第i行第j列的格子上放的是哪个零件。
如果无解,输出单独的一个字符串‘No solution’(不要引号,请注意大小写)。
所有的数据保证最多只有一组解。
.
..
…
….
…..
…..C
…CCC.
EEEHH…
E.HHH….
E………
B
BK
BKK
BJKK
JJJDD
GJGDDC
GGGCCCI
EEEHHIIA
ELHHHIAAF
ELLLLIFFFF
搜索练习题?
出题人丧心病狂?
这就是人性的沦落?
毒瘤搜索题简直变态。。。
题解一定很简单
把每种零件的旋转全部搞出来
然后爆搜一下就行了
多简单啊
多简单啊….
多…..简单….啊……….
多………………….简…….单……………………..啊…………….
特别注意一下第五个测试点
.
..
...
....
.....
......
.......
.......J
......JJJ
.......J..
如果只是单纯的爆搜判断 Nosolution 会 TLE
可以提前算出每个联通块的大小,如果小于 3 判无解,这样就能够 AC 了
这是我人生写的最长的代码( 11kb )
没有之一
如果不会写这道题目,请仔细研究代码
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define ll long long
#define RG register
const int PY=20;
int vis[500][500];
bool used[200];
bool Try[500][500];
int trn[13]={0,4,2,8,1,4,8,4,8,8,1,4,8};
void outp()
{
for(int i=1;i<=10;++i,puts(""))
for(int j=1;j<=i;++j)
putchar(vis[i][j+PY]+64);
}
bool blk(int x,int y,int k,int fy)
{
if(k==1)
{
if(fy==1)return !(vis[x][y]+vis[x][y+1]+vis[x+1][y]);
if(fy==2)return !(vis[x][y]+vis[x][y+1]+vis[x+1][y+1]);
if(fy==3)return !(vis[x][y]+vis[x+1][y]+vis[x+1][y-1]);
if(fy==4)return !(vis[x][y]+vis[x+1][y]+vis[x+1][y+1]);
}
if(k==2)
{
if(fy==1)return !(vis[x][y]+vis[x][y+1]+vis[x][y+2]+vis[x][y+3]);
if(fy==2)return !(vis[x][y]+vis[x+1][y]+vis[x+2][y]+vis[x+3][y]);
}
if(k==3)
{
if(fy==1)return !(vis[x][y]+vis[x][y+1]+vis[x][y+2]+vis[x+1][y]);
if(fy==2)return !(vis[x][y]+vis[x][y+1]+vis[x+1][y+1]+vis[x+2][y+1]);
if(fy==3)return !(vis[x][y]+vis[x+1][y]+vis[x+1][y-1]+vis[x+1][y-2]);
if(fy==4)return !(vis[x][y]+vis[x+1][y]+vis[x+2][y]+vis[x+2][y+1]);
if(fy==5)return !(vis[x][y]+vis[x+1][y]+vis[x+1][y+1]+vis[x+1][y+2]);
if(fy==6)return !(vis[x][y]+vis[x][y+1]+vis[x+1][y]+vis[x+2][y]);
if(fy==7)return !(vis[x][y]+vis[x][y+1]+vis[x][y+2]+vis[x+1][y+2]);
if(fy==8)return !(vis[x][y]+vis[x+1][y]+vis[x+2][y]+vis[x+2][y-1]);
}
if(k==4)
{
if(fy==1)return !(vis[x][y]+vis[x+1][y]+vis[x][y+1]+vis[x+1][y+1]);
}
if(k==5)
{
if(fy==1)return !(vis[x][y]+vis[x+1][y]+vis[x+2][y]+vis[x+2][y+1]+vis[x+2][y+2]);
if(fy==2)return !(vis[x][y]+vis[x][y+1]+vis[x][y+2]+vis[x+1][y]+vis[x+2][y]);
if(fy==3)return !(vis[x][y]+vis[x][y+1]+vis[x][y+2]+vis[x+1][y+2]+vis[x+2][y+2]);
if(fy==4)return !(vis[x][y]+vis[x+1][y]+vis[x+2][y]+vis[x+2][y-1]+vis[x+2][y-2]);
}
if(k==6)
{
if(fy==1)return !(vis[x][y]+vis[x][y+1]+vis[x][y+2]+vis[x][y+3]+vis[x+1][y+1]);
if(fy==2)return !(vis[x][y]+vis[x+1][y]+vis[x+2][y]+vis[x+3][y]+vis[x+1][y-1]);
if(fy==3)return !(vis[x][y]+vis[x+1][y+1]+vis[x+1][y-1]+vis[x+1][y]+vis[x+1][y-2]);
if(fy==4)return !(vis[x][y]+vis[x+1][y]+vis[x+2][y]+vis[x+3][y]+vis[x+2][y+1]);
if(fy==5)return !(vis[x][y]+vis[x][y+1]+vis[x][y+2]+vis[x][y+3]+vis[x+1][y+2]);
if(fy==6)return !(vis[x][y]+vis[x+1][y]+vis[x+2][y]+vis[x+3][y]+vis[x+2][y-1]);
if(fy==7)return !(vis[x][y]+vis[x+1][y-1]+vis[x+1][y]+vis[x+1][y+1]+vis[x+1][y+2]);
if(fy==8)return !(vis[x][y]+vis[x+1][y]+vis[x+2][y]+vis[x+3][y]+vis[x+1][y+1]);
}
if(k==7)
{
if(fy==1)return !(vis[x][y]+vis[x][y+1]+vis[x][y+2]+vis[x+1][y]+vis[x+1][y+2]);
if(fy==2)return !(vis[x][y]+vis[x][y+1]+vis[x+1][y+1]+vis[x+2][y+1]+vis[x+2][y]);
if(fy==3)return !(vis[x][y]+vis[x][y+2]+vis[x+1][y]+vis[x+1][y+1]+vis[x+1][y+2]);
if(fy==4)return !(vis[x][y]+vis[x][y+1]+vis[x+1][y]+vis[x+2][y]+vis[x+2][y+1]);
}
if(k==8)
{
if(fy==1)return !(vis[x][y]+vis[x][y+1]+vis[x][y+2]+vis[x+1][y]+vis[x+1][y+1]);
if(fy==2)return !(vis[x][y]+vis[x][y+1]+vis[x+1][y]+vis[x+1][y+1]+vis[x+2][y+1]);
if(fy==3)return !(vis[x][y]+vis[x][y+1]+vis[x+1][y-1]+vis[x+1][y]+vis[x+1][y+1]);
if(fy==4)return !(vis[x][y]+vis[x+1][y]+vis[x+1][y+1]+vis[x+2][y]+vis[x+2][y+1]);
if(fy==5)return !(vis[x][y]+vis[x+1][y]+vis[x][y+1]+vis[x+1][y+1]+vis[x+1][y+2]);
if(fy==6)return !(vis[x][y]+vis[x+1][y]+vis[x][y+1]+vis[x+1][y+1]+vis[x+2][y]);
if(fy==7)return !(vis[x][y]+vis[x][y+1]+vis[x][y+2]+vis[x+1][y+1]+vis[x+1][y+2]);
if(fy==8)return !(vis[x][y]+vis[x+1][y-1]+vis[x+1][y]+vis[x+2][y-1]+vis[x+2][y]);
}
if(k==9)
{
if(fy==1)return !(vis[x][y]+vis[x][y+1]+vis[x][y+2]+vis[x+1][y+2]+vis[x+1][y+3]);
if(fy==2)return !(vis[x][y]+vis[x+1][y]+vis[x+2][y]+vis[x+2][y-1]+vis[x+3][y-1]);
if(fy==3)return !(vis[x][y]+vis[x][y+1]+vis[x+1][y+1]+vis[x+1][y+2]+vis[x+1][y+3]);
if(fy==4)return !(vis[x][y]+vis[x+1][y]+vis[x+1][y-1]+vis[x+2][y-1]+vis[x+3][y-1]);
if(fy==5)return !(vis[x][y]+vis[x][y+1]+vis[x+1][y]+vis[x+1][y-1]+vis[x+1][y-2]);
if(fy==6)return !(vis[x][y]+vis[x+1][y]+vis[x+2][y]+vis[x+2][y+1]+vis[x+3][y+1]);
if(fy==7)return !(vis[x][y]+vis[x+1][y]+vis[x+1][y-1]+vis[x][y+1]+vis[x][y+2]);
if(fy==8)return !(vis[x][y]+vis[x+1][y]+vis[x+1][y+1]+vis[x+2][y+1]+vis[x+3][y+1]);
}
if(k==10)
{
if(fy==1)return !(vis[x][y]+vis[x+1][y-1]+vis[x+1][y]+vis[x+1][y+1]+vis[x+2][y]);
}
if(k==11)
{
if(fy==1)return !(vis[x][y]+vis[x+1][y]+vis[x+1][y+1]+vis[x+2][y+1]+vis[x+2][y+2]);
if(fy==2)return !(vis[x][y]+vis[x][y+1]+vis[x+1][y]+vis[x+1][y-1]+vis[x+2][y-1]);
if(fy==3)return !(vis[x][y]+vis[x][y+1]+vis[x+1][y+1]+vis[x+1][y+2]+vis[x+2][y+2]);
if(fy==4)return !(vis[x][y]+vis[x+1][y]+vis[x+1][y-1]+vis[x+2][y-1]+vis[x+2][y-2]);
}
if(k==12)
{
if(fy==1)return !(vis[x][y]+vis[x][y+1]+vis[x][y+2]+vis[x][y+3]+vis[x+1][y]);
if(fy==2)return !(vis[x][y]+vis[x][y+1]+vis[x+1][y+1]+vis[x+2][y+1]+vis[x+3][y+1]);
if(fy==3)return !(vis[x][y]+vis[x+1][y]+vis[x+1][y-1]+vis[x+1][y-2]+vis[x+1][y-3]);
if(fy==4)return !(vis[x][y]+vis[x+1][y]+vis[x+2][y]+vis[x+3][y]+vis[x+3][y+1]);
if(fy==5)return !(vis[x][y]+vis[x+1][y]+vis[x+1][y+1]+vis[x+1][y+2]+vis[x+1][y+3]);
if(fy==6)return !(vis[x][y]+vis[x][y+1]+vis[x+1][y]+vis[x+2][y]+vis[x+3][y]);
if(fy==7)return !(vis[x][y]+vis[x][y+1]+vis[x][y+2]+vis[x][y+3]+vis[x+1][y+3]);
if(fy==8)return !(vis[x][y]+vis[x+1][y]+vis[x+2][y]+vis[x+3][y]+vis[x+3][y-1]);
}
return false;
}
void Equ(int x,int y,int k,int fy,int w)
{
if(k==1)
{
if(fy==1)vis[x][y]=vis[x][y+1]=vis[x+1][y]=w;
if(fy==2)vis[x][y]=vis[x][y+1]=vis[x+1][y+1]=w;
if(fy==3)vis[x][y]=vis[x+1][y]=vis[x+1][y-1]=w;
if(fy==4)vis[x][y]=vis[x+1][y]=vis[x+1][y+1]=w;
}
if(k==2)
{
if(fy==1)vis[x][y]=vis[x][y+1]=vis[x][y+2]=vis[x][y+3]=w;
if(fy==2)vis[x][y]=vis[x+1][y]=vis[x+2][y]=vis[x+3][y]=w;
}
if(k==3)
{
if(fy==1)vis[x][y]=vis[x][y+1]=vis[x][y+2]=vis[x+1][y]=w;
if(fy==2)vis[x][y]=vis[x][y+1]=vis[x+1][y+1]=vis[x+2][y+1]=w;
if(fy==3)vis[x][y]=vis[x+1][y]=vis[x+1][y-1]=vis[x+1][y-2]=w;
if(fy==4)vis[x][y]=vis[x+1][y]=vis[x+2][y]=vis[x+2][y+1]=w;
if(fy==5)vis[x][y]=vis[x+1][y]=vis[x+1][y+1]=vis[x+1][y+2]=w;
if(fy==6)vis[x][y]=vis[x][y+1]=vis[x+1][y]=vis[x+2][y]=w;
if(fy==7)vis[x][y]=vis[x][y+1]=vis[x][y+2]=vis[x+1][y+2]=w;
if(fy==8)vis[x][y]=vis[x+1][y]=vis[x+2][y]=vis[x+2][y-1]=w;
}
if(k==4)
{
if(fy==1)vis[x][y]=vis[x+1][y]=vis[x][y+1]=vis[x+1][y+1]=w;
}
if(k==5)
{
if(fy==1)vis[x][y]=vis[x+1][y]=vis[x+2][y]=vis[x+2][y+1]=vis[x+2][y+2]=w;
if(fy==2)vis[x][y]=vis[x][y+1]=vis[x][y+2]=vis[x+1][y]=vis[x+2][y]=w;
if(fy==3)vis[x][y]=vis[x][y+1]=vis[x][y+2]=vis[x+1][y+2]=vis[x+2][y+2]=w;
if(fy==4)vis[x][y]=vis[x+1][y]=vis[x+2][y]=vis[x+2][y-1]=vis[x+2][y-2]=w;
}
if(k==6)
{
if(fy==1)vis[x][y]=vis[x][y+1]=vis[x][y+2]=vis[x][y+3]=vis[x+1][y+1]=w;
if(fy==2)vis[x][y]=vis[x+1][y]=vis[x+2][y]=vis[x+3][y]=vis[x+1][y-1]=w;
if(fy==3)vis[x][y]=vis[x+1][y+1]=vis[x+1][y-1]=vis[x+1][y]=vis[x+1][y-2]=w;
if(fy==4)vis[x][y]=vis[x+1][y]=vis[x+2][y]=vis[x+3][y]=vis[x+2][y+1]=w;
if(fy==5)vis[x][y]=vis[x][y+1]=vis[x][y+2]=vis[x][y+3]=vis[x+1][y+2]=w;
if(fy==6)vis[x][y]=vis[x+1][y]=vis[x+2][y]=vis[x+3][y]=vis[x+2][y-1]=w;
if(fy==7)vis[x][y]=vis[x+1][y-1]=vis[x+1][y]=vis[x+1][y+1]=vis[x+1][y+2]=w;
if(fy==8)vis[x][y]=vis[x+1][y]=vis[x+2][y]=vis[x+3][y]=vis[x+1][y+1]=w;
}
if(k==7)
{
if(fy==1)vis[x][y]=vis[x][y+1]=vis[x][y+2]=vis[x+1][y]=vis[x+1][y+2]=w;
if(fy==2)vis[x][y]=vis[x][y+1]=vis[x+1][y+1]=vis[x+2][y+1]=vis[x+2][y]=w;
if(fy==3)vis[x][y]=vis[x][y+2]=vis[x+1][y]=vis[x+1][y+1]=vis[x+1][y+2]=w;
if(fy==4)vis[x][y]=vis[x][y+1]=vis[x+1][y]=vis[x+2][y]=vis[x+2][y+1]=w;
}
if(k==8)
{
if(fy==1)vis[x][y]=vis[x][y+1]=vis[x][y+2]=vis[x+1][y]=vis[x+1][y+1]=w;
if(fy==2)vis[x][y]=vis[x][y+1]=vis[x+1][y]=vis[x+1][y+1]=vis[x+2][y+1]=w;
if(fy==3)vis[x][y]=vis[x][y+1]=vis[x+1][y-1]=vis[x+1][y]=vis[x+1][y+1]=w;
if(fy==4)vis[x][y]=vis[x+1][y]=vis[x+1][y+1]=vis[x+2][y]=vis[x+2][y+1]=w;
if(fy==5)vis[x][y]=vis[x+1][y]=vis[x][y+1]=vis[x+1][y+1]=vis[x+1][y+2]=w;
if(fy==6)vis[x][y]=vis[x+1][y]=vis[x][y+1]=vis[x+1][y+1]=vis[x+2][y]=w;
if(fy==7)vis[x][y]=vis[x][y+1]=vis[x][y+2]=vis[x+1][y+1]=vis[x+1][y+2]=w;
if(fy==8)vis[x][y]=vis[x+1][y-1]=vis[x+1][y]=vis[x+2][y-1]=vis[x+2][y]=w;
}
if(k==9)
{
if(fy==1)vis[x][y]=vis[x][y+1]=vis[x][y+2]=vis[x+1][y+2]=vis[x+1][y+3]=w;
if(fy==2)vis[x][y]=vis[x+1][y]=vis[x+2][y]=vis[x+2][y-1]=vis[x+3][y-1]=w;
if(fy==3)vis[x][y]=vis[x][y+1]=vis[x+1][y+1]=vis[x+1][y+2]=vis[x+1][y+3]=w;
if(fy==4)vis[x][y]=vis[x+1][y]=vis[x+1][y-1]=vis[x+2][y-1]=vis[x+3][y-1]=w;
if(fy==5)vis[x][y]=vis[x][y+1]=vis[x+1][y]=vis[x+1][y-1]=vis[x+1][y-2]=w;
if(fy==6)vis[x][y]=vis[x+1][y]=vis[x+2][y]=vis[x+2][y+1]=vis[x+3][y+1]=w;
if(fy==7)vis[x][y]=vis[x+1][y]=vis[x+1][y-1]=vis[x][y+1]=vis[x][y+2]=w;
if(fy==8)vis[x][y]=vis[x+1][y]=vis[x+1][y+1]=vis[x+2][y+1]=vis[x+3][y+1]=w;
}
if(k==10)
{
if(fy==1)vis[x][y]=vis[x+1][y-1]=vis[x+1][y]=vis[x+1][y+1]=vis[x+2][y]=w;
}
if(k==11)
{
if(fy==1)vis[x][y]=vis[x+1][y]=vis[x+1][y+1]=vis[x+2][y+1]=vis[x+2][y+2]=w;
if(fy==2)vis[x][y]=vis[x][y+1]=vis[x+1][y]=vis[x+1][y-1]=vis[x+2][y-1]=w;
if(fy==3)vis[x][y]=vis[x][y+1]=vis[x+1][y+1]=vis[x+1][y+2]=vis[x+2][y+2]=w;
if(fy==4)vis[x][y]=vis[x+1][y]=vis[x+1][y-1]=vis[x+2][y-1]=vis[x+2][y-2]=w;
}
if(k==12)
{
if(fy==1)vis[x][y]=vis[x][y+1]=vis[x][y+2]=vis[x][y+3]=vis[x+1][y]=w;
if(fy==2)vis[x][y]=vis[x][y+1]=vis[x+1][y+1]=vis[x+2][y+1]=vis[x+3][y+1]=w;
if(fy==3)vis[x][y]=vis[x+1][y]=vis[x+1][y-1]=vis[x+1][y-2]=vis[x+1][y-3]=w;
if(fy==4)vis[x][y]=vis[x+1][y]=vis[x+2][y]=vis[x+3][y]=vis[x+3][y+1]=w;
if(fy==5)vis[x][y]=vis[x+1][y]=vis[x+1][y+1]=vis[x+1][y+2]=vis[x+1][y+3]=w;
if(fy==6)vis[x][y]=vis[x][y+1]=vis[x+1][y]=vis[x+2][y]=vis[x+3][y]=w;
if(fy==7)vis[x][y]=vis[x][y+1]=vis[x][y+2]=vis[x][y+3]=vis[x+1][y+3]=w;
if(fy==8)vis[x][y]=vis[x+1][y]=vis[x+2][y]=vis[x+3][y]=vis[x+3][y-1]=w;
}
}
void dfs(int x,int y)
{
if(x==11){outp();exit(0);}
if(y-PY>x){dfs(x+1,1+PY);return;}
if(vis[x][y]){dfs(x,y+1);return;}
for(int i=1;i<=12;++i)
{
if(used[i])continue;
for(int j=1;j<=trn[i];++j)
if(blk(x,y,i,j))
{
Equ(x,y,i,j,i);
used[i]=true;
dfs(x,y+1);
Equ(x,y,i,j,0);
used[i]=false;
}
}
}
int ret=0;
void Fill(int x,int y)
{
if(vis[x][y])return;
if(Try[x][y])return;
ret++;
Try[x][y]=true;
Fill(x+1,y);Fill(x-1,y);Fill(x,y+1);Fill(x,y-1);
}
int main()
{
for(int i=0;i<=40;++i)
for(int j=0;j<=40;++j)
vis[i][j]=2222;
char ch[50];
for(int i=1;i<=10;++i)
{
scanf("%s",ch+1);
for(int j=1;j<=i;++j)
if(ch[j]=='.')vis[i][j+PY]=0;
else used[ch[j]-64]=true,vis[i][j+PY]=ch[j]-64;
}
for(int i=1;i<=10;++i)
for(int j=1;j<=i;++j)
if(!Try[i][j+PY]&&!vis[i][j+PY])
{
ret=0;
Fill(i,j+PY);
if(ret<3){puts("No solution");return 0;}
}
dfs(1,1+PY);
puts("No solution");
return 0;
}