Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

甩发代码,注意king有8个攻击范围,就是这个地方我WA了,改过来就AC了

Posted by kuaichenyang at 2015-11-20 00:02:26 on Problem 2734
#include <stdio.h>
#include <string.h>

const int _MAXN=1005;

int n,m;
bool used[_MAXN][_MAXN];
int map[_MAXN][_MAXN];
int q,k,p;

void queen(int x,int y){
	for(int i=y+1;i<=m&&map[x][i]==0;i++){
		used[x][i]=true;
	}
	for(int i=y-1;i>=1&&map[x][i]==0;i--){
		used[x][i]=true;
	}
	for(int i=x+1;i<=n&&map[i][y]==0;i++){
		used[i][y]=true;
	}
	for(int i=x-1;i>=1&&map[i][y]==0;i--){
		used[i][y]=true;
	}
	for(int k=1;x+k<=n&&y+k<=m&&map[x+k][y+k]==0;k++){
		used[x+k][y+k]=true;
	}
	for(int k=1;x-k>=1&&y-k>=1&&map[x-k][y-k]==0;k++){
		used[x-k][y-k]=true;
	}
	for(int k=1;x-k>=1&&y+k<=m&&map[x-k][y+k]==0;k++){
		used[x-k][y+k]=true;
	}
	for(int k=1;x+k<=n&&y-k>=1&&map[x+k][y-k]==0;k++){
		used[x+k][y-k]=true;
	}
}

void king(int x,int y){
	if(x+2<=n){
		if(y+1<=m){
			used[x+2][y+1]=true;
		}
		if(y-1>=1){
			used[x+2][y-1]=true;
		}
	}
	if(x-2>=1){
		if(y+1<=m){
			used[x-2][y+1]=true;
		}
		if(y-1>=1){
			used[x-2][y-1]=true;
		}
	}
	if(x+1<=n){
		if(y+2<=m){
			used[x+1][y+2]=true;
		}
		if(y-2>=1){
			used[x+1][y-2]=true;
		}
	}
	if(x-1>=1){
		if(y+2<=m){
			used[x-1][y+2]=true;
		}
		if(y-2>=1){
			used[x-1][y-2]=true;
		}
	}
}

int main(){
	int caseCount=1;
	while(scanf("%d%d",&n,&m),n+m!=0){
		//init
		memset(used,false,sizeof(used));
		memset(map,0,sizeof(map));
		scanf("%d",&q);
		for(int i=0;i<q;i++){
			int x,y;
			scanf("%d%d",&x,&y);
			map[x][y]=1;
			used[x][y]=true;
		}
		scanf("%d",&k);
		for(int i=0;i<k;i++){
			int x,y;
			scanf("%d%d",&x,&y);
			map[x][y]=2;
			used[x][y]=true;
		}
		scanf("%d",&p);
		for(int i=0;i<p;i++){
			int x,y;
			scanf("%d%d",&x,&y);
			map[x][y]=3;
			used[x][y]=true;
		}

		//work
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				if(map[i][j]==1){
					queen(i,j);
					continue;
				}
				if(map[i][j]==2){
					king(i,j);
					continue;
				}
			}
		}

		//print
		int ans=0;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				if(used[i][j]==false){
					++ans;
				}
			}
		}
		printf("Board %d has %d safe squares.\n",caseCount++,ans);
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator