A - Adjacent Squares

文章描述了一个编程问题,要求编写一个程序来计算给定网格中与指定位置(R,C)的方格共享边界的方格数量。程序处理不同情况,包括边界条件和输入验证。
摘要由CSDN通过智能技术生成

Problem Statement

There is a grid with H horizontal rows and W vertical columns. Let (i,j) denote the square at the i-th row from the top and the j-th column from the left.
Find the number of squares that share a side with Square R,C).

Here, two squares (a,b) and (c,d) are said to share a side if and only if ∣a−c∣+∣b−d∣=1 (where ∣x∣ denotes the absolute value of x).

Constraints

  • All values in input are integers.
  • 1≤R≤H≤10
  • 1≤C≤W≤10

Input

Input is given from Standard Input in the following format:

H W
R C

Output

Print the answer as an integer.

Sample 1

InputcopyOutputcopy
3 4
2 2
4

We will describe Sample Inputs/Outputs 1,21,2, and 33 at once below Sample Output 33.

Sample 2

InputcopyOutputcopy
3 4
1 3
3

Sample 3

InputcopyOutputcopy
3 4
3 4
2

When H=3 and W=4, the grid looks as follows.

  • For Sample Input 1, there are 4 squares adjacent to Square 2,2).
  • For Sample Input 2, there are 3 squares adjacent to Square(1,3).
  • For Sample Input 3, there are 2 squares adjacent to Square (3,4).

Sample 4

InputcopyOutputcopy
1 10
1 5
2

Sample 5

InputcopyOutputcopy
8 1
8 1
1

Sample 6

InputcopyOutputcopy
1 1
1 1
0
#include <iostream>
#include <math.h>
using namespace std;
/*输入有两行:总行列:(H,W)和正方形所在位置(a,b) 
  要输出与正方形相邻正方形的总数 
  1.普通情况: H>a && W>b;
  	循环,i<=R,j<=W,(i-a)+(j-b)=1;cout++ 
  2.H=a && W>b;
  	同1
  3.H>a && W=b
  	同1
  4.H=a=1 &7 W=b=1
  	0
*/
int main()
{
	int array1[2];
	int array2[2];
	int i,j;
	int count=0;
	for(i=0;i<2;i++){
		cin>>array1[i];
	}
	for(i=0;i<2;i++){
		cin>>array2[i];
	}
	if(array1[0]==array2[0] && array1[1]==array2[1] && array1[0]==1 && array1[1]==1)cout<<0;
	else{
		for(i=1;i<=array1[0];i++){
			for(j=1;j<=array1[1];j++){
				if(abs(i-array2[0])+abs(j-array2[1])==1)count++;
			}
		}
		cout<<count<<endl;
	}
	
	return 0;
}

  • 25
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以使用Python编写一个扫雷游戏,其中包括一个GUI(图形用户界面)和游戏逻辑。以下是一个简单的示例: ```python import tkinter as tk import random # 游戏设置 NUM_ROWS = 10 NUM_COLS = 10 NUM_MINES = 10 # 创建窗口 window = tk.Tk() window.title('Minesweeper') # 创建游戏面板 board = tk.Frame(window) board.pack() # 创建方块 squares = [] for row in range(NUM_ROWS): for col in range(NUM_COLS): square = tk.Button(board, width=2, height=1) square.grid(row=row, column=col) squares.append(square) # 添加地雷 mines = random.sample(range(NUM_ROWS * NUM_COLS), NUM_MINES) for mine in mines: squares[mine].mine = True # 点击方块时的动作 def click_square(event): square = event.widget if square.mine: square.config(text='*', bg='red') for s in squares: if s.mine: s.config(text='*', bg='red') tk.messagebox.showerror('Game Over', 'You clicked on a mine!') window.destroy() else: square.config(bg='gray') row, col = get_square_position(square) num_mines = count_adjacent_mines(row, col) if num_mines > 0: square.config(text=str(num_mines)) else: reveal_adjacent_squares(row, col) # 获取方块的位置 def get_square_position(square): for i in range(len(squares)): if squares[i] == square: row = i // NUM_COLS col = i % NUM_COLS return row, col # 计算相邻的地雷数量 def count_adjacent_mines(row, col): count = 0 for r in range(max(row - 1, 0), min(row + 2, NUM_ROWS)): for c in range(max(col - 1, 0), min(col + 2, NUM_COLS)): if squares[r * NUM_COLS + c].mine: count += 1 return count # 显示相邻的方块 def reveal_adjacent_squares(row, col): for r in range(max(row - 1, 0), min(row + 2, NUM_ROWS)): for c in range(max(col - 1, 0), min(col + 2, NUM_COLS)): if not squares[r * NUM_COLS + c].mine and squares[r * NUM_COLS + c]['bg'] == '': num_mines = count_adjacent_mines(r, c) if num_mines > 0: squares[r * NUM_COLS + c].config(text=str(num_mines), bg='gray') else: squares[r * NUM_COLS + c].config(bg='gray') reveal_adjacent_squares(r, c) # 绑定点击事件 for square in squares: square.bind('<Button-1>', click_square) # 运行游戏 window.mainloop() ``` 这个示例代码创建了一个10x10的扫雷游戏,其中有10个地雷。玩家可以点击方块来揭示它们,并尝试避免揭示地雷。如果玩家揭示到地雷,则游戏结束。 请注意,这只是一个简单的示例,实际上你可以根据自己的需求来修改和扩展这个代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值