//this low-intermediate level takes three hours to ac including 2.5 hours to inefficiently
//find out the reasons,which really broke new record of my worst.
#include <iostream>
#include<cstring>
#include <vector>
#include <stdio.h>
using namespace std;
const int N=1002;
int g[N][N]; //error1: misuse of memset。
int n,m;
struct chess{
int x,y;
chess(int a,int b){
x=a;
y=b;
}
};
void Queen(int a,int b)
{
for (int i=a-1,j=b;i>=1;i--) //error2,big mistake initiate the wrong value of i.
{
if(g[i][j]==2) break;
g[i][j]=3;
}
for (int i=a+1,j=b;i<=n;i++)
{
if(g[i][j]==2) break;
g[i][j]=3;
}
for (int i=a,j=b-1;j>=1;j--)
{
if(g[i][j]==2) break;
g[i][j]=3;
}
for (int i=a,j=b+1;j<=m;j++)
{
if(g[i][j]==2) break;
g[i][j]=3;
}
for (int i=a-1,j=b-1;i>=1&&j>=1;i--,j--)
{
if(g[i][j]==2) break;
g[i][j]=3;
}
for (int i=a+1,j=b+1;i<=n&&j<=m;i++,j++)
{
if(g[i][j]==2) break;
g[i][j]=3;
}
for (int i=a-1,j=b+1;i>=1&&j<=m;i--,j++)
{
if(g[i][j]==2) break;
g[i][j]=3;
}
for (int i=a+1,j=b-1;i<=n&&j>=1;i++,j--)
{
if(g[i][j]==2) break;
g[i][j]=3;
}
}
void Knight(int a,int b)
{
int xx[]={-2,-2,-1,1,2,2,1,-1}; //error3,xx[]
int yy[]={-1,1,2,2,1,-1,-2,-2};
for (int i=0;i<8;i++)
{
int qa=a+xx[i];
int qb=b+yy[i];
if(qa>=1&&qa<=n&&qb>=1&&qb<=m&&g[qa][qb]==0) //error4 have considered but not implemented the judgement of over broader
{
g[qa][qb]=3;
}
}
}
int main()
{
int num,a,b;
int outpu=1;
while(cin>>n>>m)
{
if (n==0&&m==0) break;
vector<chess>che; //error5 serious misuse of vector!especially two-dimension vector[][]!
vector<chess>che2;
memset(g,0,sizeof(g)); //error 6: misuse of memset.
int count=3;
while (count--) // mistake7 unsure of use of while!
{
cin>>num;
for (int i=0;i<num;i++)
{
cin>>a>>b;
g[a][b]=2;
if(count==2)
che.push_back(chess(a,b)); //
else if(count==1)
che2.push_back(chess(a,b)); //error8,add one more i++ cause input error!
}
}
for (int i=0;i<che.size();i++)
{
chess temp=che[i];
int xx=temp.x;
int yy=temp.y;
Queen(xx,yy);
}
for (int i=0;i<che2.size();i++)
{
chess temp=che2[i];
int x=temp.x;
int y=temp.y;
Knight(x,y);
}
int cou=0;
for (int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if (g[i][j]==3||g[i][j]==2) //error9 forget add a situation when the block is occupied by chess itself
{
cou++;
}
}
}
int abc=m*n-cou;
printf("Board %d has %d safe squares.\n",outpu++,abc);
}
return 0;
}
1172 Queens & Knights & Pawns
最新推荐文章于 2014-01-17 10:54:15 发布