1172 Queens & Knights & Pawns

//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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值