-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoj0213.cpp
120 lines (109 loc) · 2.8 KB
/
aoj0213.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <stdio.h>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <set>
#include <math.h>
#include <utility>
#include <stack>
#include <string.h>
#include <complex>
using namespace std;
const int INF = 1<<29;
const double EPS = 1e-8;
typedef vector<int> vec;
typedef pair<int,int> P;
int field[10][10];
int ans[10][10];
int kukaku[15];
P start[15];
int X,Y,n;
void copy_field(){
for(int i=0;i<Y;i++){
for(int j=0;j<X;j++){
ans[i][j] = field[i][j];
}
}
}
void print_field(){
for(int i=0;i<Y;i++){
cout << ans[i][0];
for(int j=1;j<X;j++){
printf(" %d",ans[i][j]);
}
printf("\n");
}
}
int dfs(int n_i){
if(n_i == n){
copy_field();
return 1;
}
int p = 0;
for(int tate=1;tate<=kukaku[n_i];tate++){
if (kukaku[n_i]%tate)continue;
int yoko = kukaku[n_i] / tate;
//printf("tate=%d yoko=%d\n",tate,yoko);
for(int y=start[n_i].first-tate+1; y<=start[n_i].first; y++){
for(int x=start[n_i].second-yoko+1; x<=start[n_i].second; x++){
//printf("n_i=%d x=%d y=%d\n",n_i,x,y);
if(!(0<=y&&y+tate-1<Y&&0<=x&&x+yoko-1<X)) continue;
bool can_paint = true;
for(int i=0;i<tate;i++){
for(int j=0;j<yoko;j++){
if(field[y+i][x+j]==0||field[y+i][x+j]==n_i+1){
field[y+i][x+j] = n_i+1;
}else{
can_paint = false;
break;
}
}
}
//printf("can_paint=%d\n",can_paint);
if(can_paint){
p += dfs(n_i+1);
if(p>1) return p;
}
for(int i=0;i<tate;i++){
for(int j=0;j<yoko;j++){
if(field[y+i][x+j]==n_i+1){
field[y+i][x+j] = 0;
}
}
}
field[start[n_i].first][start[n_i].second] = n_i+1;
}
}
}
return p;
}
int main(){
while(1){
scanf("%d%d%d",&X,&Y,&n);
if(!X)break;
for(int i=0;i<n;i++){
int d;
scanf("%d",&d);
scanf("%d",&kukaku[d-1]);
}
for(int i=0;i<Y;i++){
for(int j=0;j<X;j++){
scanf("%d",&field[i][j]);
if(field[i][j]>0){
start[field[i][j]-1] = P(i,j);
}
}
}
if(dfs(0)==1){
print_field();
}else{
printf("NA\n");
}
//break;
}
return 0;
}