输出格式输出格式输出格式!UVaOJ的输出控制让我想起了玩黑魂时的感觉。
这道题难点在M操作后的输出 “Black - ” 一眼看去‘-’后面是两个个空格,然而事实是-与后面的数字一共占3列。你自己调试的时候,发现怎样都对,而提交总会无限Presentation error。当你花半天udebug的时候,对比结果会告诉你“我这里不是两个空格!哈哈!没想到吧!“[白眼][白眼]。
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
string cc[12] ={""};
int aa[12][12];
int a[8] = {1,1,1,0,0,-1,-1,-1};
int b[8] = {-1,0,1,1,-1,0,1,-1};
int lg(int u);
int mo(char c1,char c2,int u);
int main()
{
int n = 0;
cin>>n;
int pp = 0;
for(int nn = 0;nn<n;++nn)
{
if(pp++)
{
cout<<endl;
}
for(int i = 1;i<=8;++i)
cin>>cc[i];
int u = 1;
char c;
cin>>c;
if(c == 'B') u = -1;
string s;
while(cin>>s && s[0]!='Q')
{
if(s[0] == 'L')
{
int f = lg(u);
if(!f) {
u=u*(-1);
cout<<"No legal move."<<endl;
}
else{
int de = 0;
for(int i = 1;i<=8;++i)
for(int j = 0;j<8;++j)
if(aa[i][j] == 1)
{
if(de++) {
cout<<' ';
}
cout<<'('<<i<<','<<j+1<<')';
}
cout<<endl;
}
for(int lp = 0;lp<12;++lp)
memset(aa[lp],0,sizeof(aa[lp]));
}
if(s[0] == 'M')
{
if(mo(s[1],s[2],u))
{
int wc = 0;
int bc = 0;
for(int i = 1;i<=8;++i)
{
for(int j = 0;j<8;++j)
{
if(cc[i][j] == 'W')
++wc;
if(cc[i][j] == 'B')
++bc;
}
}
cout<<"Black - ";
if(bc/10 < 1)
cout<<' ';
cout<<bc<<' ';
cout<<"White - ";
if(wc/10 < 1)
cout<<' ';
cout<<wc<<endl;
};
u = u*(-1);
}
}
for(int i = 1;i<=8;++i)
cout<<cc[i]<<endl;
}
}
int lg(int u)
{
char e;
char v;
e = u==1?'W':'B';
v = u==1?'B':'W';
for(int i = 1;i<=8;++i)
{
for(int j = 0;j<8;++j)
{
if(cc[i][j] == e)
{
for(int x = 0;x<8;++x)
{
if(i+a[x]>=1 && i+a[x]<=8 && j+b[x]>=0 && j+b[x] <=7)
{
if(cc[i+a[x]][j+b[x]] == v)
{
int ax = i+a[x];
int ay = j+b[x];
while((ax>=1 && ax<=8 && ay>=0 && ay<=7) && cc[ax][ay] == v)
{
ax+=a[x];
ay+=b[x];
}
if((ax>=1 && ax<=8 && ay>=0 && ay<=7) && cc[ax][ay] == '-')
aa[ax][ay] = 1;
}
}
}
}
}
}
int fag = 0;
for(int i = 1;i<=8;++i)
for(int j = 0;j<8;++j)
if(aa[i][j] == 1)
fag = 1;
if(fag == 0)
return 0;
return 1;
}
int mo(char c1,char c2,int u)
{
int ae = c1-'0';
int as = c2-'0';
char e;
char v;
e = u==1?'W':'B';
v = u==1?'B':'W';
cc[ae][as-1] = e;
for(int x = 0;x<8;++x)
{
int cn = 0;
if(ae+a[x]>=1 && ae+a[x]<=8 && as-1+b[x]>=0 && as-1+b[x] <=7)
{
if(cc[ae+a[x]][as-1+b[x]] == v)
{
int ax = ae+a[x];
int ay = as-1+b[x];
++cn;
while((ax>=1 && ax<=8 && ay>=0 && ay<=7) && cc[ax][ay] == v)
{
ax+=a[x];
ay+=b[x];
++cn;
}
if((ax>=1 && ax<=8 && ay>=0 && ay<=7) && cc[ax][ay] == e)
{
for(int i =1;i<cn;++i)
cc[ae+i*a[x]][as-1+i*b[x]] = e;
}
}
}
}
return 1;
}