问题描述
- uva 220 一直Time Limited exceeded!!!
-
不知道为什么在网站上就是Time Limited exceeded,样例测试都没有问题,能不能帮忙看看是什么问题?
#include
#include
#include
using namespace std;
#define maxsize 10
char board[maxsize][maxsize];
int cnt[130]={0};
int ki[8]={-1,-1,-1,0,0,1,1,1};
int kj[8]={-1,0,1,-1,1,-1,0,1};
int fir=1,x,y;void get_board()
{
memset(board,0,sizeof board);
memset(cnt,0,sizeof cnt);
for(int i=1;i<=8;i++)
{
for(int j=1;j<=8;j++)
{
cin>>board[i][j];
cnt[board[i][j]]++;
}
char ch=getchar();
}
}void is_legal(char &cur,char &next,int flag)
{
int first=1;for(int i=1;i<=8;i++) for(int j=1;j<=8;j++) { if(board[i][j]=='-') { for(int l=0;l<8;l++) { for(int k=1;;k++) { if(board[i+k*ki[l]][j+k*kj[l]]==next) continue; if(board[i+k*ki[l]][j+k*kj[l]]==cur&&k!=1) { if(!flag) { if(first) first=0; else cout<<" "; cout<<"("<<i<<","<<j<<")"; } else { if(i==x&&j==y) { for(int t=0;t<k;t++) board[i+t*ki[l]][j+t*kj[l]]=cur; cnt[cur]+=k; cnt[next]-=k-1; cout<<"Black - "<<cnt['B']<<" White - "<<cnt['W']<<endl; return ; } } } else break; } } } } if(!flag) { if(first) cout<<"No legal move."; cout<<endl; } else { swap(next,cur); is_legal(cur,next,flag); }
}
void LM(char &cur,int flag)
{
char next;
if(cur=='B')
next='W';
else next='B';
if(flag){
int a;
cin>>a;
x=a/10;
y=a%10;
}
is_legal(cur,next,flag);
if(flag)
swap(cur,next);
}void Quit()
{
for(int i=1;i<=8;i++)
{
for(int j=1;j<=8;j++)
{
cout<<board[i][j];
}
cout<<endl;
}
}int main()
{
int t;
scanf("%d",&t);
while(t--)
{
if(fir)
fir=0;
else cout<<endl;
while(getchar()!='n');get_board(); char cur=getchar(); char cmd; for(;;) { cmd=getchar(); cmd=getchar(); switch(cmd) { case'L':LM(cur,0); break; case'M':LM(cur,1); break; case'Q':Quit(); break; } if(cmd=='Q') break; } } return 0;
}