Codeforces Round #385 (Div. 2) 745B Hongcow Solves A Puzzle
2017-07-18 17:54
441 查看
B. Hongcow Solves A Puzzle
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Hongcow likes solving puzzles.
One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid
of characters, where the character 'X' denotes a part of the puzzle and '.'
denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified.
The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot
overlap.
You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in
mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position.
Input
The first line of input will contain two integers n and m (1 ≤ n, m ≤ 500),
the dimensions of the puzzle piece.
The next n lines will describe the jigsaw piece. Each line will have length m and
will consist of characters '.' and 'X' only. 'X'
corresponds to a part of the puzzle piece, '.' is an empty space.
It is guaranteed there is at least one 'X' character in the input and that the 'X'
characters form a 4-connected region.
Output
Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO"
otherwise.
Examples
input
output
input
output
input
output
Note
For the first sample, one example of a rectangle we can form is as follows
For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle.
In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle:
题意:给你两块一模一样的,由x组成的拼图。为你能不能把这两块拼图组成一个矩形(矩形中间不能空)。这两个拼图不能旋转,且不会重合。
思路:只要判断X的区域是不是矩形即可。如果不是矩形,则不能拼成矩形。
扫一遍图形,确定上下左右边界。扫一遍即可。
#include<bits/stdc++.h>
using namespace std;
char str[505][505];
int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
for(int i=0;i<n;i++)
{
scanf("%s",str[i]);
}
int u=99999,d=0,l=999999,r=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(str[i][j]=='X')///确定边界,u,d,l,r分别是上下左右
{
if(i<=u)
{
u=i;
}
if(i>=d)
{
d=i;
}
if(j<=l)
{
l=j;
}
if(j>=r)
{
r=j;
}
}
}
}
int flag=1;
for(int i=u;i<=d;i++)
{
for(int j=l;j<=r;j++)
{
if(str[i][j]!='X')///扫描一遍
{
flag=0;
}
}
}
if(flag)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
return 0;
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Hongcow likes solving puzzles.
One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid
of characters, where the character 'X' denotes a part of the puzzle and '.'
denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified.
The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot
overlap.
You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in
mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position.
Input
The first line of input will contain two integers n and m (1 ≤ n, m ≤ 500),
the dimensions of the puzzle piece.
The next n lines will describe the jigsaw piece. Each line will have length m and
will consist of characters '.' and 'X' only. 'X'
corresponds to a part of the puzzle piece, '.' is an empty space.
It is guaranteed there is at least one 'X' character in the input and that the 'X'
characters form a 4-connected region.
Output
Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO"
otherwise.
Examples
input
2 3 XXX XXX
output
YES
input
2 2 .X XX
output
NO
input
5 5 ..... ..X.. ..... ..... .....
output
YES
Note
For the first sample, one example of a rectangle we can form is as follows
111222 111222
For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle.
In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle:
..... ..XX. ..... ..... .....
题意:给你两块一模一样的,由x组成的拼图。为你能不能把这两块拼图组成一个矩形(矩形中间不能空)。这两个拼图不能旋转,且不会重合。
思路:只要判断X的区域是不是矩形即可。如果不是矩形,则不能拼成矩形。
扫一遍图形,确定上下左右边界。扫一遍即可。
#include<bits/stdc++.h>
using namespace std;
char str[505][505];
int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
for(int i=0;i<n;i++)
{
scanf("%s",str[i]);
}
int u=99999,d=0,l=999999,r=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(str[i][j]=='X')///确定边界,u,d,l,r分别是上下左右
{
if(i<=u)
{
u=i;
}
if(i>=d)
{
d=i;
}
if(j<=l)
{
l=j;
}
if(j>=r)
{
r=j;
}
}
}
}
int flag=1;
for(int i=u;i<=d;i++)
{
for(int j=l;j<=r;j++)
{
if(str[i][j]!='X')///扫描一遍
{
flag=0;
}
}
}
if(flag)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
return 0;
}
相关文章推荐
- 【37.50%】【codeforces 745B】Hongcow Solves A Puzzle
- Codeforces Round #385 (Div. 2) -- B. Hongcow Solves A Puzzle (判断是否是矩形,水题)
- Codeforces Round #385 (Div. 2)B. Hongcow Solves A Puzzle【思维+暴力】
- Codeforces Round #385 (Div. 2) B. Hongcow Solves A Puzzle 几何、思维题
- Codeforces Round #385 (Div. 2) B. Hongcow Solves A Puzzle
- Hongcow Solves A Puzzle CodeForces - 745B
- Codeforces Round #385 (Div. 2) B - Hongcow Solves A Puzzle 暴力
- B. Hongcow Solves A Puzzle
- 【Codeforces 745 B Hongcow Solves A Puzzle】
- 745 B. Hongcow Solves A Puzzle codeforces
- Hongcow Solves A Puzzle (思维)
- Codeforces Round #385 (Div. 1)A. Hongcow Builds A Nation
- Codeforces Round #385 (Div. 2) -- C. Hongcow Builds A Nation(并查集)
- Codeforces Round #385 (Div. 2)A.Hongcow Learns the Cyclic Shift【暴力】水题
- Codeforces Round #385 (Div. 1) C. Hongcow Buys a Deck of Cards
- Codeforces Round #385 (Div. 2) C. Hongcow Builds A Nation 并查集+贪心+组合学、图论、dfs
- Codeforces Round #385 (Div. 1) B. Hongcow's Game(bitmask)
- Codeforces Round #385 (Div. 2)C. Hongcow Builds A Nation【并查集+贪心】好题~
- Codeforces Round #385 (Div. 2) E. Hongcow Buys a Deck of Cards DP+好题
- Codeforces Round #385 (Div. 2) A. Hongcow Learns the Cyclic Shift 水题