Time limit | 2 seconds |
---|---|
Memory limit | 256 megabytes |
Little girl Masha is looking at the wall in her room. The wall is tiled with square tiles, but some of the tiles are replaced with lamps. So it is possible to consider the wall to be a rectangle of n × m, some cells contain tiles, other cells contain lamps.
Masha has paints of k different colors. Consider continuous vertical or horizontal segments of tiles, having an edge of the wall or a lamp at each of its ends. Masha wants to paint all tiles in such way, that any such segment has all tiles painted in different colors. Masha will not paint lamps. She doesn't have to use all the colors.
Help Masha to paint the wall.
Input format |
Input contains several test cases. The first line contains the number of test cases t. Each test case is described by several lines. The first line contains three integers: n, m, k (1 ≤ n, m ≤ 100, 1 ≤ k ≤ max(n, m)) — the size of the wall and the number of paints Masha has. The following n lines contain m integers aij each:
The total number of tiles and lamps in all test cases of one input doesn't exceed 105. |
---|---|
Output format |
For each test case first print the answer:
|
Examples |
Input data
2 4 3 2 0 1 0 1 0 1 1 0 1 0 1 0 3 4 2 0 1 0 1 1 0 1 1 1 1 1 0 Output data
YES 0 2 0 2 0 2 1 0 1 0 1 0 NO |
思路:涂成形如
1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4这样就保证不会同一行和列出现相同,可以先涂满整个地图再把0位置的放回原位,或者老老实实地bfs。
# include <bits/stdc++.h>
using namespace std;
int vis[103][103], a[103][103];
int t, n, m, k;
int dx[4]={0,1,0,-1}, dy[4]={1,0,-1,0};
struct node
{
int x, y, col, dir;
}q[10003];
bool dfs(int x, int y, int pre, int d)
{
if(x<0||y<0||x>=n||y>=m||a[x][y]==0) return true;
if(d>k) {return false;}
if(pre!=-1)
return dfs(x+dx[pre], y+dy[pre],pre,d+1);
for(int i=0; i<4; ++i)
{
int mx=x+dx[i], my=y+dy[i];
if(!dfs(mx,my,i,d+1))
return false;
}
return true;
}
void bfs(int x, int y)
{
int l=0, r=0;
q[r++] = node{x,y,0,-1};
vis[x][y] = 1;
a[x][y] = 0;
while(l<r)
{
node u=q[l];
++l;
for(int i=0; i<4; ++i)
{
int mx = u.x+dx[i], my=u.y+dy[i];
if(mx<0||my<0||mx>=n||my>=m||a[mx][my]==0||vis[mx][my]) continue;
vis[mx][my] = 1;
int col;
if(i==0||i==1) col=(u.col+1)%k;
else col=(u.col+k-1)%k;
q[r++] = node{mx,my,col};
a[mx][my] = col;
}
}
}
int main()
{
scanf("%d",&t);
while(t--)
{
bool flag = true;
scanf("%d%d%d",&n,&m,&k);
for(int i=0; i<n; ++i)
for(int j=0; j<m; ++j)
scanf("%d",&a[i][j]);
memset(vis, 0, sizeof(vis));
for(int i=0; i<n; ++i)
{
for(int j=0; j<m; ++j)
{
if(a[i][j]==0) continue;
if(!dfs(i,j,-1,1))
{
flag = false;
break;
}
}
if(!flag) break;
}
if(!flag)
{
puts("NO");
continue;
}
for(int i=0; i<n; ++i)
{
for(int j=0; j<m; ++j)
{
if(a[i][j]!=0&&!vis[i][j])
{
bfs(i,j);
}
}
}
puts("YES");
for(int i=0; i<n; ++i)
{
for(int j=0; j<m; ++j)
{
printf("%d%c",vis[i][j]?a[i][j]+1:0, j==m?'\n':' ');
}
puts("");
}
}
return 0;
}
close