B - Hongcow Solves A Puzzle
题目连接:
http://codeforces.com/contest/745/problem/B
Description
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.
Sample Input
2 3
XXX
XXX
Sample Output
YES
Hint
题意
题意很迷,问你复制一遍X的这个图形,然后通过平移,能否构成一个大矩形,要求不能重叠……
翻译过来就是:问你这个图里面的X是否恰好构成的是一个矩形。
题解:
n4可能会T,那么我们就n3预处理,然后n^2check就好了嘛
代码
#include<bits/stdc++.h>
using namespace std;
int n,m;
string s[505];
int mp[505][505];
int vis[505][505];
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
cin>>s[i];
int stx=0,sty=0;
int len1 = 0;
int len2 = 0;
int flag = 0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(s[i][j]=='X'){
stx=i,sty=j;
for(int k=0;k+j<m;k++){
if(s[i][j+k]=='X')len1++;
else break;
}
for(int k=0;k+i<n;k++){
if(s[i+k][j]=='X')len2++;
else break;
}
flag = 1;
break;
}
}
if(flag)break;
}
flag = 0;
for(int i=0;i<len2;i++){
for(int j=0;j<len1;j++){
if(s[stx+i][sty+j]!='X')
flag = 1;
vis[stx+i][sty+j]=1;
}
}
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(s[i][j]=='X'&&vis[i][j]==0)
flag = 1;
if(flag)cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}