2302: 【入门】两个数相邻吗?

内存限制:128 MB 时间限制:1.000 S
评测方式:文本比较 命题人:
提交:1 解决:1

题目描述

同学们在操场上排成了一个 lns="http://www.w3.org/1998/Math/MathML"> 行 lns="http://www.w3.org/1998/Math/MathML"> 列的队形,假设这个队形中所有人年龄都不同,那么给定 lns="http://www.w3.org/1998/Math/MathML">2 个年龄后,请你编程判断,这 lns="http://www.w3.org/1998/Math/MathML">2 个年龄对应的同学是否相邻?

相邻的定义是:如果两个同学在上下左右的位置是挨在一起的,那么就算是相邻的。

比如:如下是一个 lns="http://www.w3.org/1998/Math/MathML">3 行 lns="http://www.w3.org/1998/Math/MathML">4 列的队形,这个队形中每个数字代表了每个同学的年龄。

8 2 3 4
5 6 7 1
9 10 11 12 

那么这个队形中,年龄 lns="http://www.w3.org/1998/Math/MathML">6 和年龄 lns="http://www.w3.org/1998/Math/MathML">10 两个值就是相邻的。

输入

第 lns="http://www.w3.org/1998/Math/MathML">1 行有 lns="http://www.w3.org/1998/Math/MathML">2 个整数 lns="http://www.w3.org/1998/Math/MathML"> 和 lns="http://www.w3.org/1998/Math/MathML"> ,分别代表队形的行和列的值( lns="http://www.w3.org/1998/Math/MathML">2,200

接下来 lns="http://www.w3.org/1998/Math/MathML"> 行,每行有 lns="http://www.w3.org/1998/Math/MathML"> 个整数,代表每个同学的年龄(每个同学的年龄的值在 lns="http://www.w3.org/1998/Math/MathML">140000 之间)

最后一行输入 lns="http://www.w3.org/1998/Math/MathML">2 个整数,代表 lns="http://www.w3.org/1998/Math/MathML">2 个不同年龄的值。

输出

如果两个年龄的值是相邻的,请输出字符 Y ,否则请输出字符 N 。

样例输入 复制

3 4
8 2 3 4
5 6 7 1
9 10 11 12
6 10

样例输出 复制

Y

提示

#include<bits/stdc++.h>
using namespace std;
const int N=210;
const int dx[]={0,1,0,-1};
const int dy[]={-1,0,1,0};
int a[N][N],n,m,age1,age2;
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++) {
    	for(int j=1;j<=m;j++) {
    		cin>>a[i][j];
		}
	}
	cin>>age1>>age2;
    for(int i=1;i<=n;i++) {
    	for(int j=1;j<=m;j++) {
    		if (a[i][j]==age1 || a[i][j]==age2) {
    		    for(int k=0;k<4;k++) {
    			    if (a[i+dx[k]][j+dy[k]]==age1 || a[i+dx[k]][j+dy[k]]==age2){
    			    	cout<<"Y";
    			    	return 0;
					}
			    }    			
			}
		}
	}
	cout<<"N";	
	return 0;
}