南邮 OJ 1390 Rooks

Rooks

时间限制(普通/Java) :  1000 MS/ 3000 MS          运行内存限制 : 65536 KByte
总提交 : 62            测试通过 : 43 

比赛描述

Jake and Sully are playing around with a chessboard one night after working with their avatars all

day. They decide it would be interesting to place some rooks on the chessboard in a way that no rook

can threaten another rook. Since rooks move along rows and columns, this means two rooks may not

be on the same row or column. Your goal is to write a program to determine whether any rooks are

threatened.




输入

Chessboards are 8x8 boards with positions between (1,1) and (8,8). The input begins with the

number of chess boards. Each chessboard is on a separate line and begins with the number of rooks,

followed by the column and row positions of each rook.


输出

For each chessboard, your program should output the words ”SAFE” or ”NOT SAFE” on a

single line.


样例输入

2
3 1 1 2 6 8 8
2 2 3 1 3

样例输出

SAFE
NOT SAFE

提示

undefined

题目来源

Internet



#include<iostream>

int main(){
	int cas,n,i,j,row,colum;
	bool safe;
	scanf("%d",&cas);
	while(cas--){
		safe = 1;
		row = colum = 0;
		scanf("%d",&n);
		while(n--){
			scanf("%d%d",&i,&j);
			if(safe){
				if(row & (1<<i)){
					safe = 0;
				}else{
					row |= (1<<i);
				}
				if(colum & (1<<j)){
					safe = 0;
				}else{
					colum |= (1<<j);
				}
			}
		}
		if(safe){
			printf("SAFE\n");
		}else{
			printf("NOT SAFE\n");
		}
	}
}







你可能感兴趣的:(ACM,南邮OJ,Rooks)