C - Tiling
Time limit : 2sec / Memory limit : 256MB
Score : 900 points
Problem Statement
Takahashi has an N×M grid, with N horizontal rows and M vertical columns. Determine if we can place A 1×2 tiles (1 vertical, 2 horizontal) and B 2×1 tiles (2vertical, 1 horizontal) satisfying the following conditions, and construct one arrangement of the tiles if it is possible:
- All the tiles must be placed on the grid.
- Tiles must not stick out of the grid, and no two different tiles may intersect.
- Neither the grid nor the tiles may be rotated.
- Every tile completely covers exactly two squares.
Constraints
- 1≤N,M≤1000
- 0≤A,B≤500000
- N, M, A and B are integers.
Input
Input is given from Standard Input in the following format:
N M A B
Output
If it is impossible to place all the tiles, print NO
. Otherwise, print the following:
YES c11…c1M : cN1…cNM
Here, cij must be one of the following characters: .
, <
, >
, ^
and v
. Represent an arrangement by using each of these characters as follows:
- When cij is
.
, it indicates that the square at the i-th row and j-th column is empty; - When cij is
<
, it indicates that the square at the i-th row and j-th column is covered by the left half of a 1×2 tile; - When cij is
>
, it indicates that the square at the i-th row and j-th column is covered by the right half of a 1×2 tile; - When cij is
^
, it indicates that the square at the i-th row and j-th column is covered by the top half of a 2×1 tile; - When cij is
v
, it indicates that the square at the i-th row and j-th column is covered by the bottom half of a 2×1 tile.
Sample Input 1
3 4 4 2
Sample Output 1
YES <><> ^<>^ v<>v
This is one example of a way to place four 1×2 tiles and three 2×1 tiles on a 3×4 grid.
Sample Input 2
4 5 5 3
Sample Output 2
YES <>..^ ^.<>v v<>.^ <><>v
Sample Input 3
7 9 20 20
Sample Output 3
NO
模拟题坑死人!!!
Code:
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#define N 1005
using namespace std;
char ans[N][N];
inline int read()
{
int x=0,f=1;char s=getchar();
while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
while(s<='9'&&s>='0'){x=x*10+s-'0';s=getchar();}
return x*f;
}
int main()
{
int n=read(),m=read(),a=read(),b=read();
memset(ans,'.',sizeof(ans));
for(int i=1;i<=n;i++)ans[i][m+1]='\0';
if(n&1)
{
int j=1;
while(a&&j+1<=m)
{
ans[n][j]='<';
ans[n][j+1]='>';
j+=2;
a--;
}
}
if(m&1)
{
int i=n;
while(b&&i>1)
{
ans[i-1][m]='^';
ans[i][m]='v';
i-=2;
b--;
}
}
for(int i=n/2;i>=1;i--)
for(int j=1;j<=m/2;j++)
{
int x=i*2-1,y=j*2-1;
if(n%2==1&&m%2==1&&a==1&&b==1&&i==1&&j==m/2)
{
ans[x][y]='^';
ans[x+1][y]='v';
b--;
ans[x][y+1]='<';
ans[x][y+2]='>';
a--;
}
if(a&&b<2)
{
ans[x][y]='<';
ans[x][y+1]='>';
a--;
if(a)
{
ans[x+1][y]='<';
ans[x+1][y+1]='>';
a--;
}
}else if(b)
{
ans[x][y]='^';
ans[x+1][y]='v';
b--;
if(b)
{
ans[x][y+1]='^';
ans[x+1][y+1]='v';
b--;
}
}
}
if(a||b)puts("NO");else
{
puts("YES");
for(int i=1;i<=n;i++)puts(ans[i]+1);
}
return 0;
}