当前位置: 首页 > news >正文

【AtCoder】Beginner Contest 377-B.Avoid Rook Attack

Problem Statement

题目链接
There is a grid of 64 64 64 squares with 8 8 8 rows and 8 8 8 columns. Let ( i , j ) (i,j) (i,j) denote the square at the i i i-th row from the top ( 1 ≤ i ≤ 8 ) (1\leq i\leq8) (1i8) and j j j-th column from the left ( 1 ≤ j ≤ 8 ) (1\leq j\leq8) (1j8).

Each square is either empty or has a piece placed on it. The state of the squares is represented by a sequence ( S 1 , S 2 , S 3 , … , S 8 ) (S_1,S_2,S_3,\ldots,S_8) (S1,S2,S3,,S8) of 8 8 8 strings of length 8 8 8. Square ( i , j ) (i,j) (i,j) ( 1 ≤ i ≤ 8 , 1 ≤ j ≤ 8 ) (1\leq i\leq8,1\leq j\leq8) (1i8,1j8) is empty if the j j j-th character of S i S_i Si is ., and has a piece if it is #.

You want to place your piece on an empty square in such a way that it cannot be captured by any of the existing pieces.

A piece placed on square ( i , j ) (i,j) (i,j) can capture pieces that satisfy either of the following conditions:

  • Placed on a square in row i i i
  • Placed on a square in column j j j

For example, a piece placed on square ( 4 , 4 ) (4,4) (4,4) can capture pieces placed on the squares shown in blue in the following figure:

How many squares can you place your piece on?

Constraints

  • Each S i S_i Si is a string of length 8 8 8 consisting of . and # ( 1 ≤ i ≤ 8 ) (1\leq i\leq 8) (1i8).

Input

The input is given from Standard Input in the following format:

S 1 S_1 S1
S 2 S_2 S2
S 3 S_3 S3
S 4 S_4 S4
S 5 S_5 S5
S 6 S_6 S6
S 7 S_7 S7
S 8 S_8 S8

Output

Print the number of empty squares where you can place your piece without it being captured by any existing pieces.

Sample Input 1

...#....
#.......
.......#
....#...
.#......
........
........
..#.....

Sample Output 1

4

The existing pieces can capture pieces placed on the squares shown in blue in the following figure:

Therefore, you can place your piece without it being captured on 4 4 4 squares: square ( 6 , 6 ) (6,6) (6,6), square ( 6 , 7 ) (6,7) (6,7), square ( 7 , 6 ) (7,6) (7,6), and square ( 7 , 7 ) (7,7) (7,7).

Sample Input 2

........
........
........
........
........
........
........
........

Sample Output 2

64

There may be no pieces on the grid.

Sample Input 3

.#......
..#..#..
....#...
........
..#....#
........
...#....
....#...

Sample Output 3

4

解法

题目中说,假如当前位置有元素,那么当前位置的一行和一列上都不能放其他元素。让我们计算出还有多少个位置可以放元素。

那么,我们使用一个二维 bool 类型的数组存储是否可以放元素。

如果位置 map[i][j] 上有元素,那么就将 st[0][j]st[i][0] 都标记为 true 表示有元素。

统计答案时,只需要有统计没有元素的位置就可以了,即 st[j][0]!st[0][i] 都为 false 即可。

具体代码如下:

在这里插入图片描述

char map[10][10];
bool st[9][9];
void solved() {/* your code */for (int i = 1; i <=8; i ++) {for (int j = 1; j <= 8; j ++) {cin >> map[i][j];if (map[i][j] == '#') {// 在列上标记st[0][j] = 1;// 在行上标记st[i][0] = 1;}}}int sum = 0;for (int i = 1; i <= 8; i ++) {for (int j = 1; j <= 8; j ++) {// 寻找行和列都为 false 的情况if(!st[j][0] && !st[0][i]) sum ++;}}cout << sum << endl;
}

总结

这道题目因为数据规模很小,所以我们可以使用二维数组存标记去题,但是如果数据量规模较大,使用二维数组存起来的方法必然失效。对于数据量规模较大的题目,下次将会分享具体做法。

http://www.lryc.cn/news/479618.html

相关文章:

  • 江协科技STM32学习- P38 软件SPI读写W25Q64
  • 【Triton 教程】低内存 Dropout
  • npx创建项目时,error fetch failed.TypeError: fetch failed
  • 《Kotlin实战》-附录
  • yelp数据集上识别潜在的热门商家
  • 【Linux】进程信号全攻略(一)
  • linux文件重命名
  • 如何选择适合的AWS EC2实例类型
  • 【Uniapp】Uniapp Android原生插件开发指北
  • 【随手笔记】FLASH-W25Q16(三)
  • 2024软件测试面试热点问题
  • 【JAVA】java 企业微信信息推送
  • 介绍一下数组(c基础)(smart 版)
  • Java项目实战II基于Spring Boot的个人云盘管理系统设计与实现(开发文档+数据库+源码)
  • 探索数据科学与大数据技术专业本科生的广阔就业前景
  • 微服务架构面试内容整理-Zuul
  • 解决Knife4j 接口界面UI中文乱码问题
  • 微服务架构面试内容整理-Sleuth
  • Go语言的接口示例
  • 【Apache ECharts】<农作物病害发生防治面积>
  • 基于vue3实现的聊天机器人前端(附代码)
  • DICOM标准:深入详解DICOM医学影像中的传输语法
  • sql server 文件备份恢复
  • Gradle命令编译Android Studio工程项目并签名
  • lua入门教程:垃圾回收
  • 基于前后端分离架构,SaaS云平台与私有云部署的智慧校园源码,java电子班牌源码
  • 知识总结五
  • 一、初识C语言(1)
  • petty 状态管理库文档
  • SpringMVC学习记录(三)之响应数据