题目链接:A - First Grid (atcoder.jp)
Problem Statement
We have a grid with 2 horizontal rows and 2 vertical columns.
Each of the squares is black or white, and there are at least 2 black squares.
The colors of the squares are given to you as strings S1 and S2, as follows.
- If the j-th character of Si is
#
, the square at the i-th row from the top and j-th column from the left is black. - If the j-th character of Si is
.
, the square at the i-th row from the top and j-th column from the left is white.
You can travel between two different black squares if and only if they share a side.
Determine whether it is possible to travel from every black square to every black square (directly or indirectly) by only passing black squares.
Constraints
- Each of S1 and S2 is a string with two characters consisting of
#
and.
. - S1 and S2 have two or more
#
s in total.
Input
Input is given from Standard Input in the following format:
S1 S2
Output
If it is possible to travel from every black square to every black square, print Yes
; otherwise, print No
.
Sample Input 1
## .#
Sample Output 1
Yes
It is possible to directly travel between the top-left and top-right black squares and between top-right and bottom-right squares.
These two moves enable us to travel from every black square to every black square, so the answer is Yes
.
Sample Input 2
.# #.
Sample Output 2
No
It is impossible to travel between the top-right and bottom-left black squares, so the answer is No
.
题意:有2行2列四个格子,每个格子都有颜色,要么黑色,要么白色,问黑格子是否全部连在一起(有一条公共边就算连在一起)
思路,只有2个黑格子的需要考虑:对角是不行的
#include<bits/stdc++.h>
using namespace std;
int main(){
string s[5];
for(int i = 0; i <= 1; i++){
cin >> s[i];
}
int ans = 0;
for(int i = 0; i < 2; i++){
for(int l = 0; l < 2; l++){
if(s[i][l] == '#'){
ans++;
}
}
}
if(ans == 2){
if(s[0][0] == '#' && s[1][1] == '#'){
cout << "No" << endl;
}else if(s[0][1] == '#' && s[1][0] == '#'){
cout << "No" << endl;
}else{
cout << "Yes" << endl;
}
}else{
cout << "Yes" << endl;
}
return 0;
}