跳到正文

题解

题解:SP26385 HARISH_PUZZLE - Harish and his rooks puzzle

Leo_29约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年6月22日
收录
2025年12月12日
更新
2026年10月31日
篇幅
约 2 分钟
获赞
1
题目
SP26385

版本

  1. v1当前

SP26385题解

主要题意

在一个 88 行 88 列的棋盘上放 88 个车,要求一个车所在的行与列上没有其他车。

现在已经给你摆好的棋盘,问摆的棋盘是否合规。

解题思路

纯模拟,我们只需要在每个车所在的行与列分别遍历一次,看有没有其他车,然后判断车的数量是否合规即可。

这道题的要点是:

  • 一个车所在的行列没有其他车
  • 一共有 88 个车。

上代码!

C++
#include <bits/stdc++.h>
using namespace std;

char ch[20][20];

bool check(int x, int y)
{
	for(int i = 1;i <= 8;i ++)
		if(ch[x][i] == ch[x][y] && y != i)
			return false;
	
	for(int i = 1;i <= 8;i ++)
		if(ch[i][y] == ch[x][y] && x != i)
			return false;
	return true;
}

int main(void)
{
	int T;
	cin >> T;
	while(T --)
	{
		
		bool fg = 0;
		for(int i = 1;i <= 8;i ++)
			for(int j = 1;j <= 8;j ++)
				cin >> ch[i][j];
		
		int cnt = 0;
		for(int i = 1;i <= 8;i ++)
			for(int j = 1;j <= 8;j ++)
				if(ch[i][j] == 'R')
				{
					if(!check(i, j))
						fg = 1;
					cnt ++;			
				}
		if(cnt != 8)
		{
			cout << "NO" << endl;
			continue;
		}

		if(!fg)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
	
	return 0;
}
原文