Problem:
>土地分割
ハウジングメーカーの山田ハウスは、新たな目玉商品として、学校や病院など環境が充実している緑ホームタウンの分譲地を売り出しました。この分譲地は複数の区画に分割されており、好きなだけ購入できますが、購入する区画を合わせた土地の形状は長方形(正方形を含む)でなければなりません。
山田ハウスは、全区画が完売した分譲地を管理するため購入者ごとに境界線を引き、その区画の 1つに購入者番号を書いた看板を設置しておきました。境界線は地面に木の枝で線を引いただけのものであったため、数日後の豪雨により消えてしまい看板だけが残りました。図 1 は看板のあった区画に、その区画を買った購入者番号を記したものです。これでは分譲地がどのように買われたか分かりません。救いは事務所の引き出しにあった購入者番号
b
と購入区画数
k
のメモ(図 2)が見つかったことです。
図１（左）：看板の配置、図2（右）：メモ
プログラマーであるあなたは、プログラムを書いて山田ハウスを助けることになりました。分譲地の大きさ
X × Y
、購入者数
n
、メモの情報
b
、
k
、及び看板の位置情報
s
を入力とし、図 3 に示すように各区画の購入者を出力するプログラムを作成してください。
図3：各区画の購入者
与えられた情報について、以下の場合は NA と出力します。
区画を区別する方法が存在しない場合
区画を区別する方法が複数存在する場合
Input
複数のデータセットの並びが入力として与えられます。入力の終わりはゼロが３つの行で示されます。各データセットは以下の形式で与えられます。
X
Y
n
b
1
k
1
b
2
k
2
:
b
n
k
n
s
11
s
21
...
s
X1
s
12
s
22
...
s
X2
:
s
1Y
s
2Y
...
s
XY
1 行目に
X, Y, n
(1 ≤
X, Y
≤ 10, 1 ≤
n
≤ 15) が与えられます。続く
n
行にメモの
i
行目に書かれた情報
b
i
(1 ≤
b
i
≤
n
),
k
i
(1 ≤
k
i
≤ 100) が与えられます。
続く
Y
行に区画情報の
i
行目の情報
s
ij
が与えられます。
s
ij
は
i
行目の左から
j
番目の区画の看板情報を表します。看板の情報
s
ij
として、その区画に看板が無い場合は 0 、看板がある場合はその区画の購入者番号が与えられます。
データセットの数は 50  を超えません。
Output
入力データセットごとに、購入者情報またはNA を出力します。
Sample Input
5 4 6
1 6
2 4
3 3
4 4
5 1
6 2
0 0 1 0 0
0 0 0 2 0
0 0 0 3 0
0 4 5 0 6
3 3 1
1 9
0 0 1
0 0 0
0 0 0
4 4 4
1 6
2 2
3 4
4 4
0 1 0 0
0 0 0 2
0 0 3 0
0 4 0 0
0 0 0
Output for the Sample Input
1 1 1 2 2
1 1 1 2 2
4 4 3 3 3
4 4 5 6 6
1 1 1
1 1 1
1 1 1
NA

Submitted solutions:

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;


bool is_valid_grid(int w, int h, const vector<vector<int>>& grid) {
	bool is_valid = true;

	for (int y = 0; y < h; y++) {
		if (!is_valid) {
			break;
		}
		for (int x = 0; x < w; x++) {
			if (grid[y][x] == 0) {
				is_valid = false;
				break;
			}
		}
	}
	return is_valid;
}

bool is_valid_location(int c_owner_idx, int cw, int ch, int top_left_x, int top_left_y, const vector<vector<int>>& grid) {
	bool is_valid = true;

	for (int y = top_left_y; y < top_left_y + ch; y++) {
		if (!is_valid) {
			break;
		}
		for (int x = top_left_x; x < top_left_x + cw; x++) {
			if (grid[y][x] != 0 && grid[y][x] != c_owner_idx) {
				is_valid = false;
				break;
			}
		}
	}
	return is_valid;
}


vector<vector<int>> search(int w, int h, unordered_map<int, int>& memo, const vector<vector<int>>& grid) {
	vector<vector<int>> owner_coord_list;

	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {
			if (grid[y][x] > 0) {
				vector<int> tmp;

				tmp.push_back(x);
				tmp.push_back(y);
				tmp.push_back(grid[y][x]);
				owner_coord_list.push_back(tmp);
			}
		}
	}

	vector<vector<int>> result_grid;
	vector<pair<int, vector<vector<int>>>> stack;
	bool is_valid = true;

	stack.push_back(pair<int, vector<vector<int>>>(0, grid));
	while (!stack.empty()) {
		auto s = stack.back();
		int c_idx = s.first;
		auto c_grid = s.second;

		stack.pop_back();
		if (c_idx == owner_coord_list.size()) {
			if (result_grid.size() > 0) {
				is_valid = false;
				break;
			}

			if (is_valid_grid(w, h, c_grid)) {
				result_grid = c_grid;
			}
			continue;
		}

		int c_owner_idx = owner_coord_list[c_idx][2];
		int cx = owner_coord_list[c_idx][0];
		int cy = owner_coord_list[c_idx][1];
		int c_num_area = memo[c_owner_idx];
		int n_idx = c_idx + 1;

		for (int cw = 1; cw <= c_num_area; cw++) {
			if (c_num_area % cw != 0) {
				continue;
			}

			int ch = c_num_area / cw;

			for (int top_left_y = cy - ch + 1; top_left_y <= cy; top_left_y++) {
				if (top_left_y < 0 || top_left_y + ch > h) {
					continue;
				}
				for (int top_left_x = cx - cw + 1; top_left_x <= cx; top_left_x++) {
					if (top_left_x < 0 || top_left_x + cw > w) {
						continue;
					}

					if (is_valid_location(c_owner_idx, cw, ch, top_left_x, top_left_y, c_grid)) {
						vector<vector<int>> n_grid = c_grid;

						for (int y = top_left_y; y < top_left_y + ch; y++) {
							for (int x = top_left_x; x < top_left_x + cw; x++) {
								n_grid[y][x] = c_owner_idx;
							}
						}
						stack.push_back(pair<int, vector<vector<int>>>(n_idx, n_grid));
					}
				}
			}
		}
	}
	return is_valid ? result_grid : vector<vector<int>>();
}


int main() {
#if READ_FROM_FILE
	ifstream ifs("test0.txt");
	cin.rdbuf(ifs.rdbuf());
#endif

	while (true) {
		int w = 0;
		int h = 0;
		int n = 0;

		cin >> w >> h >> n;

		if (w == 0 && h == 0 && n == 0) {
			break;
		}

		unordered_map<int, int> memo;

		for (int i = 0; i < n; i++) {
			int b = 0;
			int k = 0;

			cin >> b >> k;
			memo[b] = k;
		}

		vector<vector<int>> grid(h, vector<int>(w));

		for (int y = 0; y < h; y++) {
			for (int x = 0; x < w; x++) {
				cin >> grid[y][x];
			}
		}

		auto result_grid = search(w, h, memo, grid);

		if (result_grid.empty()) {
			cout << "NA" << endl;
		}
		else {
			for (int y = 0; y < h; y++) {
				cout << result_grid[y][0];
				for (int x = 1; x < w; x++) {
					cout << " " << result_grid[y][x];
				}
				cout << endl;
			}
		}
	}
	return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 3180
final_rank: 11

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;


bool is_valid_grid(int w, int h, const vector<vector<int>>& grid) {
	bool is_valid = true;

	for (int y = 0; y < h; y++) {
		if (!is_valid) {
			break;
		}
		for (int x = 0; x < w; x++) {
			if (grid[y][x] == 0) {
				is_valid = false;
				break;
			}
		}
	}
	return is_valid;
}

bool is_valid_location(int c_owner_idx, int cw, int ch, int top_left_x, int top_left_y, const vector<vector<int>>& grid) {
	bool is_valid = true;

	for (int y = top_left_y; y < top_left_y + ch; y++) {
		if (!is_valid) {
			break;
		}
		for (int x = top_left_x; x < top_left_x + cw; x++) {
			if (grid[y][x] != 0 && grid[y][x] != c_owner_idx) {
				is_valid = false;
				break;
			}
		}
	}
	return is_valid;
}


vector<vector<int>> search(int w, int h, unordered_map<int, int>& memo, const vector<vector<int>>& grid) {
	vector<vector<int>> owner_coord_list;

	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {
			if (grid[y][x] > 0) {
				vector<int> tmp;
				tmp.push_back(x);
				tmp.push_back(y);
				tmp.push_back(grid[y][x]);
				owner_coord_list.push_back(tmp);
			}
		}
	}

	vector<vector<int>> result_grid;
	vector<pair<int, vector<vector<int>>>> stack;
	bool is_valid = true;

	stack.push_back(pair<int, vector<vector<int>>>(0, grid));
	while (!stack.empty()) {
		auto s = stack.back();
		int c_idx = s.first;
		auto c_grid = s.second;

		stack.pop_back();
		if (c_idx == owner_coord_list.size()) {
			if (result_grid.size() > 0) {
				is_valid = false;
				break;
			}

			if (is_valid_grid(w, h, c_grid)) {
				result_grid = c_grid;
			}
			continue;
		}

		int c_owner_idx = owner_coord_list[c_idx][2];
		int cx = owner_coord_list[c_idx][0];
		int cy = owner_coord_list[c_idx][1];
		int c_num_area = memo[c_owner_idx];
		int n_idx = c_idx + 1;

		for (int cw = 1; cw <= c_num_area; cw++) {
			if (c_num_area % cw != 0) {
				continue;
			}

			int ch = c_num_area / cw;

			for (int top_left_y = cy - ch + 1; top_left_y <= cy; top_left_y++) {
				if (top_left_y < 0 || top_left_y + ch > h) {
					continue;
				}
				for (int top_left_x = cx - cw + 1; top_left_x <= cx; top_left_x++) {
					if (top_left_x < 0 || top_left_x + cw > w) {
						continue;
					}

					if (is_valid_location(c_owner_idx, cw, ch, top_left_x, top_left_y, c_grid)) {
						vector<vector<int>> n_grid = c_grid;

						for (int y = top_left_y; y < top_left_y + ch; y++) {
							for (int x = top_left_x; x < top_left_x + cw; x++) {
								n_grid[y][x] = c_owner_idx;
							}
						}
						stack.push_back(pair<int, vector<vector<int>>>(n_idx, n_grid));
					}
				}
			}
		}
	}
	return is_valid ? result_grid : vector<vector<int>>();
}


int main() {
#if READ_FROM_FILE
	ifstream ifs("test0.txt");
	cin.rdbuf(ifs.rdbuf());
#endif

	while (true) {
		int w = 0;
		int h = 0;
		int n = 0;

		cin >> w >> h >> n;

		if (w == 0 && h == 0 && n == 0) {
			break;
		}

		unordered_map<int, int> memo;

		for (int i = 0; i < n; i++) {
			int b = 0;
			int k = 0;

			cin >> b >> k;
			memo[b] = k;
		}

		vector<vector<int>> grid(h, vector<int>(w));

		for (int y = 0; y < h; y++) {
			for (int x = 0; x < w; x++) {
				cin >> grid[y][x];
			}
		}

		auto result_grid = search(w, h, memo, grid);
		if (result_grid.empty()) {
			cout << "NA" << endl;
		}
		else {
			for (int y = 0; y < h; y++) {
				cout << result_grid[y][0];
				for (int x = 1; x < w; x++) {
					cout << " " << result_grid[y][x];
				}
				cout << endl;
			}
		}
	}
	return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 3480
final_rank: 13

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;


bool is_valid_grid(int w, int h, const vector<vector<int>>& grid) {
	bool is_valid = true;
	for (int y = 0; y < h; y++) {
		if (!is_valid) {
			break;
		}
		for (int x = 0; x < w; x++) {
			if (grid[y][x] == 0) {
				is_valid = false;
				break;
			}
		}
	}
	return is_valid;
}

bool is_valid_location(int c_owner_idx, int cw, int ch, int top_left_x, int top_left_y, const vector<vector<int>>& grid) {
	bool is_valid = true;
	
	for (int y = top_left_y; y < top_left_y + ch; y++) {
		if (!is_valid) {
			break;
		}
		for (int x = top_left_x; x < top_left_x + cw; x++) {
			if (grid[y][x] != 0 && grid[y][x] != c_owner_idx) {
				is_valid = false;
				break;
			}
		}
	}
	return is_valid;
}


vector<vector<int>> search(int w, int h, unordered_map<int, int>& memo, const vector<vector<int>>& grid) {
	vector<vector<int>> owner_coord_list;

	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {
			if (grid[y][x] > 0) {
				vector<int> tmp;
				tmp.push_back(x);
				tmp.push_back(y);
				tmp.push_back(grid[y][x]);
				owner_coord_list.push_back(tmp);
			}
		}
	}
	
	vector<vector<int>> result_grid;
	vector<pair<int, vector<vector<int>>>> stack;
	bool is_valid = true;

	stack.push_back(pair<int, vector<vector<int>>> (0, grid));
	while (!stack.empty()) {
		auto s = stack.back();
		int c_idx = s.first;
		auto c_grid = s.second;

		stack.pop_back();
		if (c_idx == owner_coord_list.size()) {
			if (result_grid.size() > 0) {
				is_valid = false;
				break;
			}
			
			if (is_valid_grid(w, h, c_grid)) {
				result_grid = c_grid;
			}
			continue;
		}

		int c_owner_idx = owner_coord_list[c_idx][2];
		int cx = owner_coord_list[c_idx][0];
		int cy = owner_coord_list[c_idx][1];
		int c_num_area = memo[c_owner_idx];
		int n_idx = c_idx + 1;

		for (int cw = 1; cw <= c_num_area; cw++) {
			if (c_num_area % cw != 0) {
				continue;
			}

			int ch = c_num_area / cw;

			for (int top_left_y = cy - ch + 1; top_left_y <= cy; top_left_y++) {
				if (top_left_y < 0 || top_left_y + ch > h) {
					continue;
				}

				for (int top_left_x = cx - cw + 1; top_left_x <= cx; top_left_x++) {
					if (top_left_x < 0 || top_left_x + cw > w) {
						continue;
					}
					
					if (is_valid_location(c_owner_idx, cw, ch, top_left_x, top_left_y, c_grid)) {
						vector<vector<int>> n_grid = c_grid;

						for (int y = top_left_y; y < top_left_y + ch; y++) {
							for (int x = top_left_x; x < top_left_x + cw; x++) {
								n_grid[y][x] = c_owner_idx;
							}
						}
						stack.push_back(pair<int, vector<vector<int>>>(n_idx, n_grid));
					}
				}
			}
		}
	}
	return is_valid ? result_grid : vector<vector<int>>();
}


int main() {
#if READ_FROM_FILE
	ifstream ifs("test0.txt");
	cin.rdbuf(ifs.rdbuf());
#endif

	while (true) {
		int w = 0;
		int h = 0;
		int n = 0;

		cin >> w >> h >> n;

		if (w == 0 && h == 0 && n == 0) {
			break;
		}

		unordered_map<int, int> memo;
		for (int i = 0; i < n; i++) {
			int b = 0;
			int k = 0;

			cin >> b >> k;
			memo[b] = k;
		}

		vector<vector<int>> grid(h, vector<int>(w));
		for (int y = 0; y < h; y++) {
			for (int x = 0; x < w; x++) {
				cin >> grid[y][x];
			}
		}

		vector<vector<int>> result_grid = search(w, h, memo, grid);
		if (result_grid.empty()) {
			cout << "NA" << endl;
		}
		else {
			for (int y = 0; y < h; y++) {
				cout << result_grid[y][0];
				for (int x = 1; x < w; x++) {
					cout << " " << result_grid[y][x];
				}
				cout << endl;
			}
		}
	}
	return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 3220
final_rank: 12

code_snippet: #include<iostream>
using namespace std;
int H,W,N;
int k[16];
int x[16],y[16];
bool found;
int s[10][10];
int ans[10][10];
bool dfs(int id)
{
	if(id>N)
	{
		if(found)return true;
		for(int i=0;i<H;i++)for(int j=0;j<W;j++)ans[i][j]=s[i][j];
		found=true;
		return false;
	}
	int sum[11][11]={};
	for(int i=0;i<H;i++)for(int j=0;j<W;j++)
	{
		sum[i+1][j+1]=sum[i+1][j]+sum[i][j+1]-sum[i][j]+(s[i][j]>0);
	}
	int K=k[id];
	for(int h=1;h<=K;h++)if(K%h==0)
	{
		int w=K/h;
		if(h>H||w>W)continue;
		for(int i=0;i+h<=H;i++)if(i<=x[id]&&x[id]<i+h)
		{
			for(int j=0;j+w<=W;j++)if(j<=y[id]&&y[id]<j+w)
			{
				if(sum[i+h][j+w]-sum[i][j+w]-sum[i+h][j]+sum[i][j]>1)continue;
				for(int I=0;I<h;I++)for(int J=0;J<w;J++)s[i+I][j+J]=id;
				if(dfs(id+1))return true;
				for(int I=0;I<h;I++)for(int J=0;J<w;J++)s[i+I][j+J]=0;
				s[x[id]][y[id]]=id;
			}
		}
	}
	return false;
}
main()
{
	while(cin>>W>>H>>N,H)
	{
		int sumk=0;
		for(int i=1;i<=N;i++)
		{
			k[i]=0;
			x[i]=y[i]=-1;
		}
		for(int i=0;i<N;i++)
		{
			int b;
			cin>>b;
			cin>>k[b];
			sumk+=k[b];
		}
		for(int i=0;i<H;i++)for(int j=0;j<W;j++)
		{
			cin>>s[i][j];
			if(s[i][j]>0)x[s[i][j]]=i,y[s[i][j]]=j;
		}
		if(sumk!=H*W)
		{
			cout<<"NA"<<endl;
			continue;
		}
		found=false;
		if(!dfs(1)&&found)
		{
			for(int i=0;i<H;i++)
			{
				for(int j=0;j<W;j++)cout<<ans[i][j]<<(j+1==W?"\n":" ");
			}
		}
		else cout<<"NA"<<endl;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3104
final_rank: 7

code_snippet: #include<iostream>
#include<algorithm>
#include <map>
#include <vector>
#include <stack>
#include<string>
#include<queue>
#include <regex>
#include<iomanip>

using namespace std;

int b[16];
int s[11][11];
pair<int, int> bPos[11];

class Area
{
public:
	int X, Y;
	int data[11][11];
	Area() { X = -1; Y = -1; }
	Area(int data[11][11], int X, int Y)
	{
		this->X = X;
		this->Y = Y;
		for (size_t i = 1; i <= Y; i++)
		{
			for (size_t j = 1; j <= X; j++)
			{
				this->data[j][i] = data[j][i];
			}
		}
	}
	Area(const Area& area)
	{
		X = area.X;
		Y = area.Y;
		if (X == -1)
			return;
		for (size_t i = 1; i <= Y; i++)
		{
			for (size_t j = 1; j <= X; j++)
			{
				data[j][i] = area.data[j][i];
			}
		}
	}
	bool CanAddRect(int x0, int y0, int wid, int hei,int num)
	{
		for (size_t i = y0; i < y0 + hei; i++)
		{
			for (size_t j = x0; j < x0 + wid; j++)
			{
				if (data[j][i] != 0 && data[j][i] != num)
					return false;
			}
		}
		return true;
	}
	void AddRect(int x0, int y0, int wid, int hei,int num)
	{
		for (size_t i = y0; i < y0 + hei; i++)
		{
			for (size_t j = x0; j < x0 + wid; j++)
			{
				data[j][i] = num;
			}
		}
	}
};

Area BFS(int X, int Y, int n)
{
	queue<Area> nodes;
	Area a0(s, X, Y);
	nodes.push(a0);
	Area nul;
	nodes.push(nul);
	int floor = 1;
	Area ret;
	while (!nodes.empty())
	{
		Area node = nodes.front();
		nodes.pop();
		if (node.X == -1)
		{
			if (nodes.empty())
				break;
			floor++;
			if (floor > n)
				break;
			Area nul;
			nodes.push(nul); 
			continue;
		}
		int pArea = b[floor];
		for (size_t i = 1; i <= pArea; i++)
		{
			if (pArea % i == 0)
			{
				int wid = i;
				int hei = pArea / wid;
				int x0 = bPos[floor].first;
				int y0 = bPos[floor].second;
				int minY = max(1, y0 - hei + 1);
				int minX = max(1, x0 - wid + 1);
				for (size_t y = minY; y <= y0; y++)
				{
					if (y + hei - 1 > Y)
						continue;
					for (size_t x = minX; x <= x0; x++)
					{
						//
						//cout << "try put " << floor << " at " << x << "," << y << " " << wid << " and " << hei << endl;
						if (x + wid - 1 <= X && node.CanAddRect(x, y, wid, hei,floor))
						{
							//
							//cout << "put " << floor << " at " << x << "," << y << " " << wid << " and " << hei << endl;
							Area newNode(node);
							newNode.AddRect(x, y, wid, hei, floor);
							nodes.push(newNode);
							if (floor == n)
							{
								if (ret.X == -1)
									ret = Area(newNode);
								else
									return Area();
							}
						}
					}
				}
			}
		}
	}
	if (floor < n)
		return Area();
	else return ret;
}

int main()
{
	int X, Y, n;
	cin >> X >> Y >> n;
	while (X)
	{
		int no;
		for (size_t i = 1; i <= n; i++)
		{
			cin >> no;
			cin >> b[no];
		}
		for (size_t i = 1; i <= Y; i++)
		{
			for (size_t j = 1; j <= X; j++)
			{
				cin >> s[j][i];
				if (s[j][i] != 0)
					bPos[s[j][i]] = make_pair(j, i);
			}
		}
		Area ret = BFS(X, Y, n);
		if (ret.X == -1)
			cout << "NA" << endl;
		else
		{
			for (size_t i = 1; i <= Y; i++)
			{
				for (size_t j = 1; j <= X; j++)
				{
					cout << ret.data[j][i];
					if (j < X)
						cout << " ";
				}
				cout << endl;
			}
		}
		cin >> X >> Y >> n;
	}
}
accuracy: 1
time_ms: 20
memory_kb: 6856
final_rank: 19

code_snippet: #include<iostream>
#include<vector>
#include<functional>
using namespace std;

struct Area{
    int si, ti, sj, tj;
};

int main(){
    int w, h, n;
    while(cin >> w >> h >> n, w){
        int cnt[n+1];
        for(int i = 1; i <= n; i++){
            int a, b;
            cin >> a >> b;
            cnt[a] = b;
        }
        string s[h][w];
        for(int i = 0; i < h*w; i++)    cin >> s[i/w][i%w];
        vector<Area> ok[n+1];
        for(int si = 0; si < h; si++){
            for(int sj = 0; sj < w; sj++){
                for(int ti = si; ti < h; ti++){
                    for(int tj = sj; tj < w; tj++){
                        string tmp = "";
                        bool valid = true;
                        for(int i = si; i <= ti; i++){
                            for(int j = sj; j <= tj; j++){
                                if(s[i][j] == "0")  continue;
                                if(tmp != "" && tmp != s[i][j]){
                                    valid = false;
                                    break;
                                }
                                tmp = s[i][j];
                            }
                            if(!valid)  break;
                        }
                        if(tmp != "" && valid && (ti-si+1)*(tj-sj+1)==cnt[stoi(tmp)]){
                            ok[stoi(tmp)].push_back(Area({si,ti,sj,tj}));
                        }
                    }
                }
            }
        }
        vector<vector<int>> ans(h, vector<int>(w, 0)), pool(h, vector<int>(w, 0));
        bool na = false;
        function<void(int)> dfs = [&](int ind)->void{
            if(ind == n+1){
                if(pool[0][0] != 0){
                    na = true;
                    return;
                }
                pool = ans;
                return;
            }
            for(Area a : ok[ind]){
                bool valid = true;
                for(int i = a.si; i <= a.ti; i++){
                    for(int j = a.sj; j <= a.tj; j++){
                        valid &= ans[i][j] == 0;
                    }
                }
                if(!valid)  continue;
                for(int i = a.si; i <= a.ti; i++){
                    for(int j = a.sj; j <= a.tj; j++){
                        ans[i][j] = ind;
                    }
                }
                dfs(ind+1);
                for(int i = a.si; i <= a.ti; i++){
                    for(int j = a.sj; j <= a.tj; j++){
                        ans[i][j] = 0;
                    }
                }
            }
        };
        dfs(1);
        if(na || pool[0][0] == 0){
            cout << "NA" << endl;
        }else{
            for(int i = 0; i < h; i++){
                for(int j = 0; j < w; j++){
                    cout << pool[i][j] << " \n"[j==w-1];
                }
            }
        }
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3192
final_rank: 8

code_snippet: #include<iostream>
#include<vector>
using namespace std;
typedef struct position{
  int x, y;
}position;
vector<vector<int> > ans2;
bool fit(vector<vector<int> > &ans, position lu, int h, int w, int val){
  for (int i = lu.y; i < lu.y + h; i++) {
    for (int j = lu.x; j < lu.x + w; j++) {
      if(ans[i][j] != 0 and ans[i][j] != val) return false;
    }
  }
  return true;
}
         
bool range(position p, int h, int w, int mh, int mw){
  return ((0 <= p.x and p.x + w - 1 < mw) and
          (0 <= p.y and p.y + h - 1 < mh));
}
           
int dfs(vector<vector<int> > &land,
        vector<vector<int> >  &ans,
         vector<int> &b,
         vector<int> &k,
         vector<position> &s,
         int depth = 0){
  if(depth == b.size()){
    if(ans2[0][0] != -1) return 100;
    ans2 = ans;
    return 1; 
  }
  // std::cout << depth << std::endl;
  int res = 0;
  for (int i = 0; i < k[depth]; i++) {
    int h = 1 + i, w = k[depth]/(i + 1);
    if((k[depth]%(i + 1)) != 0) continue;
    for (int j = 0; j < k[depth]; j++) {
      position lu = s[b[depth]];
      lu.x -= j%w;
      lu.y -= j/w;
      // std::cout << "lu.x:" << lu.x << " lu.y:" << lu.y << std::endl;
      // std::cout << "h:" << h << " w:" << w << std::endl;
      // std::cout << "range:" << range(lu, h, w, land.size(), land[0].size()) << std::endl;
      if(not range(lu, h, w, land.size(), land[0].size())) continue;
      if(fit(ans, lu, h, w, b[depth])) {
        // ansの更新
        for (int k = lu.y; k < lu.y + h; k++) 
          for (int l = lu.x; l < lu.x + w; l++) 
            ans[k][l] = b[depth];

        // for (int i = 0; i < ans.size(); i++) {
        //   for (int j = 0; j < ans[i].size(); j++) {
        //     std::cout << ans[i][j];
        //     if(j != ans[i].size() - 1) std::cout << " ";
        //     else std::cout << std::endl;
        //   }
        // }
        // std::cout << std::endl;

        res += dfs(land, ans, b, k, s, depth + 1);
        // ansを戻す．
        for (int k = lu.y; k < lu.y + h; k++) {
          for (int l = lu.x; l < lu.x + w; l++) {
            ans[k][l] = 0;
            if(k == s[b[depth]].y and l == s[b[depth]].x)ans[k][l] = b[depth];
          }
        }
      }
    }
  }
  return res;
}

int main(){
  int x, y, n;
  ans2.resize(1);
  ans2[0].resize(1, -1);
  while(std::cin >> x >> y >> n){
    if(x == 0) break;
    ans2[0][0] = -1;
    vector<int> b(n), k(n);
    vector<position> s(n + 1, position{-1, -1});
    vector<vector<int> > land(y, vector<int>(x));
    for (int i = 0; i < n; i++) {
      std::cin >> b[i] >> k[i];
    }
    for (int i = 0; i < y; i++) {
      for (int j = 0; j < x; j++) {
        std::cin >> land[i][j];
        if(land[i][j] != 0) s[land[i][j]] = position{j, i};
      }
    }
    vector<vector<int> > ans = land;
    if(dfs(land, ans, b, k, s) == 1){
      for (int i = 0; i < y; i++) {
        for (int j = 0; j < x; j++) {
          std::cout << ans2[i][j];
          if(j != x - 1) std::cout << " ";
          else std::cout << std::endl;
        }
      }
    }else{
      std::cout << "NA" << std::endl;
    }
  }
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3160
final_rank: 9

code_snippet: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
#define chmin(a, b) ((a) = min((a), (b)))
#define chmax(a, b) ((a) = max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const double pi = acos(-1);
const double eps = 1e-10;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};

int x, y, n;
int ans;
vector<vector<int>> fians(20, vector<int>(20));
vector<vector<P>> divi(20);
vector<P> larger;

void dfs(int depth, vector<vector<int>> &s){
    if(depth == n){
        ans++;
        fians = s;
        return ;
    }

    if(1 < ans){
        return ;
    }

    int K = larger[depth].first;
    int B = larger[depth].second;

    for(auto i:divi[B]){
        int h = i.first;
        int w = i.second;

        for(int j=0; j+h-1<y; j++){
            for(int k=0; k+w-1<x; k++){
                int l = k;
                int r = k + w - 1;
                int u = j;
                int d = j + h - 1;

                bool exist = false;
                bool valid = true;
                auto cs = s;
                for(int i1=u; i1<=d; i1++){
                    for(int i2=l; i2<=r; i2++){
                        if(cs[i1][i2] == B){
                            exist = true;
                        }
                        else if(cs[i1][i2] != 0){
                            valid = false;
                            goto END;
                        }
                    }
                }

                END:;

                if(exist && valid){
                    for(int i1=u; i1<=d; i1++){
                        for(int i2=l; i2<=r; i2++){
                            cs[i1][i2] = B;
                        }
                    }

                    dfs(depth + 1, cs);
                }
            }
        }
    }

    return ;
}

int main(){
    while(1){
        ans = 0;
        larger.clear();
        for(int i=0; i<20; i++){
            divi[i].clear();
        }

        cin>>x>>y>>n;
        if(x == 0) return 0;
        
        int sum = 0;
        for(int i=0; i<n; i++){
            int b, k; cin>>b>>k;
            larger.emplace_back(k, b);
            sum += k;
            set<P> st;
            for(int j=1; j*j<=k; j++){
                if(k % j == 0){
                    st.emplace(j, k / j);
                    st.emplace(k / j, j);
                }
            }

            for(auto j:st){
                divi[b].emplace_back(j.first, j.second);
            }
        }

        sort(larger.begin(), larger.end(), greater<P>());

        vector<vector<int>> s(y, vector<int>(x));
        for(int i=0; i<y; i++){
            for(int j=0; j<x; j++){
                cin>>s[i][j];
            }
        }

        if(x * y != sum){
            cout << "NA" << endl;
            continue;
        }

        dfs(0, s);

        if(ans == 1){
            for(int i=0; i<y; i++){
                for(int j=0; j<x; j++){
                    cout << fians[i][j] << " \n"[j == x-1];
                }
            }
        }
        else{
            cout << "NA" << endl;
        }
    }
}
accuracy: 1
time_ms: 110
memory_kb: 3184
final_rank: 14

code_snippet: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
 
using namespace std;
 
#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
 
template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }
 
typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int W, H, N; 
int b[16], k[16];
int s[10][10];
int ans[10][10];
int used[10][10];
int TX[20], TY[20];
class DFSError {};
void dfs(int idx){
  if(idx == N) {
    if(ans[0][0] != -1) throw DFSError();
    REP(y, H)REP(x, W) ans[y][x] = used[y][x];
    return;
  }
  /*
  printf("idx = %d\n", idx);
  REP(y, H){
    REP(x, W){
      cout << used[y][x];
      if(x != W - 1) cout << " ";
      else cout << endl;
    }
  }
  */
  int tx = TX[b[idx] - 1];
  int ty = TY[b[idx] - 1];
  //printf("idx = %d tx = %d ty = %d\n", idx, tx, ty);
   
  assert(idx < N);
  assert(b[idx] >= 1 && b[idx] <= N);
  assert(tx != -1);
  for(int h = 1; h <= k[idx]; h++) if(k[idx] % h == 0){
    int w = k[idx] / h;
    for(int by = 0; by > -h; by--) for(int bx = 0; bx > -w; bx--){
      int sx = tx + bx;
      int sy = ty + by;
      bool ok = true;
      for(int y = 0; y < h; y++) for(int x = 0; x < w; x++) {
        if(!valid(sx + x, sy + y, W, H) || used[sy + y][sx + x] != -1) ok = false;
      }
      if(ok){
        int tmp[10][10];
        memcpy(tmp, used, sizeof(used));
        for(int y = 0; y < h; y++) for(int x = 0; x < w; x++) {
          used[sy + y][sx + x] = b[idx];
        }
        dfs(idx + 1);
        memcpy(used, tmp, sizeof(used));
      }
    }
  }
}
 
int main(){
  while(cin >> W >> H >> N && (W | H | N)){
    REP(i, N) cin >> b[i] >> k[i];
    REP(y, H)REP(x, W) cin >> s[y][x];
    try{
      memset(used, -1, sizeof(used));
      memset(ans, -1, sizeof(ans));
      memset(TX, -1, sizeof(TX));
      memset(TY, -1, sizeof(TY));
      REP(y, H) REP(x, W) if(s[y][x] > 0) TX[s[y][x] - 1] = x, TY[s[y][x] - 1] = y;
      REP(i, N)if(TX[i] == -1){
        /*
        cerr << W << " " << H << " " << N << endl;
        REP(j, N) cerr << b[j] << " " << k[j] << endl;
        REP(y, H) { REP(x, W) cerr << s[y][x] << " "; cerr << endl; }
        assert("bord is dissapeared" == "");
        */
        throw 0;
      }
      dfs(0);
      if(ans[0][0] == -1) throw DFSError();
      REP(y, H){
        REP(x, W){
          cout << ans[y][x];
          if(x != W - 1) cout << " ";
          else cout << endl;
        }
      }
    }catch(...){
      cout << "NA" << endl;
    }
  }
  return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 3072
final_rank: 15

code_snippet: #include <iostream>
#include <vector>

using vec = std::vector<int>;
using Land = std::vector<std::vector<int>>;

int W, H, N;
Land s;
std::vector<Land> res;

bool check(int h, int w, int a, int b, int num)
{
    if (h + a > H || w + b > W) {
        return false;
    }
    for (int i = h; i < h + a; i++) {
        for (int j = w; j < w + b; j++) {
            if (s[i][j] != 0 && s[i][j] != num) {
                return false;
            }
        }
    }

    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (h <= i && i < h + a && w <= j && j < w + b) {
                continue;
            }
            if (s[i][j] == num) {
                return false;
            }
        }
    }
    
    return true;
}

void paint(int h, int w, int a, int b, int num)
{
    for (int i = h; i < h + a; i++) {
        for (int j = w; j < w + b; j++) {
            s[i][j] = num;
        }
    }
}

void dfs(int idx, const vec &b, const vec &k)
{
    if (idx == N) {
        res.emplace_back(s);        
        return;
    }    
    
    for (int i = 1; i <= k[idx]; i++) {
        int j = k[idx] / i;
        if (i * j != k[idx]) continue;
        for (int h = 0; h < H; h++) {
            for (int w = 0; w < W; w++) {                
                if (check(h, w, i, j, b[idx])) {
                    auto tmp = s;
                    paint(h, w, i, j, b[idx]);
                    dfs(idx + 1, b, k);
                    s = tmp;
                }
            }
        }
    }
}

void print(Land &l)
{
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (j > 0) std::cout << " ";            
            std::cout << l[i][j];
        }
        std::cout << std::endl;
    }
}

void init()
{
    s.assign(H, std::vector<int>(W));
    res.clear();
}

int main()
{
    while (std::cin >> W >> H >> N, W) {
        init();
        vec b(N), k(N);
        for (int i = 0; i < N; i++) {
            std::cin >> b[i] >> k[i];
        }

        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                std::cin >> s[i][j];
            }
        }

        dfs(0, b, k);

        if (res.size() != 1) {
            std::cout << "NA" << std::endl;
        } else {
            print(res[0]);
        }
    }
    return 0;
}
accuracy: 1
time_ms: 200
memory_kb: 3040
final_rank: 16

code_snippet: #include<iostream>
using namespace std;
struct State{int x[12][12];};
int H,W,n,b[20],p[20];State S,ans;
int area(State V,int q){
	int mx=100000,my=100000,nx=-1,ny=-1;
	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			if(V.x[i][j]!=q)continue;
			mx=min(mx,i);my=min(my,j);nx=max(nx,i);ny=max(ny,j);
		}
	}
	for(int i=mx;i<=nx;i++){
		for(int j=my;j<=ny;j++){
			if(V.x[i][j]!=q && V.x[i][j]!=0)return 99999999;
		}
	}
	return (nx-mx+1)*(ny-my+1);
}
int solve(State T,int r1,int r2){
	if(r2==W){r1++;r2=0;}
	if(r1==H){
		for(int i=1;i<=n;i++){
			if(area(T,i)!=p[i])return 0;
		}
		ans=T;return 1;
	}
	if(T.x[r1][r2]>=1){return solve(T,r1,r2+1);}
	else{
		int ret=0;
		for(int i=1;i<=n;i++){
			State U=T;U.x[r1][r2]=i;
			if(area(U,i)>p[i])continue;
			ret+=solve(U,r1,r2+1);
		}
		return ret;
	}
}
int main(){
	while(true){
		cin>>W>>H>>n;if(n==0)break;
		for(int i=0;i<n;i++){cin>>b[i];cin>>p[b[i]];}
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++)cin>>S.x[i][j];
		}
		int ret=solve(S,0,0);
		if(ret==1){
			for(int i=0;i<H;i++){
				for(int j=0;j<W;j++){
					if(j)cout<<' ';cout<<ans.x[i][j];
				}
				cout<<endl;
			}
		}
		else cout<<"NA"<<endl;
	}
	return 0;
}
accuracy: 1
time_ms: 330
memory_kb: 3104
final_rank: 20

code_snippet: #include "bits/stdc++.h"
#include<unordered_map>
#pragma warning(disable:4996)
using namespace std;
vector<long long int> divisor(long long int n) {
	vector<long long int> res;
	for (long long int i = 1; i * i <= n; ++i) {
		if (n % i == 0) {
			res.push_back(i);
			if (i * i != n) res.push_back(n / i);
		}
	}
	sort(begin(res), end(res));
	return res;
}
int X, Y, n;
vector<vector<int>>getans(const vector<vector<int>>&field, const int now, const vector<int>&nums,const vector<pair<int,int>>&ps) {
	if (now == ps.size())return field;
	vector<long long int>divs(divisor(nums[now]));
	vector<vector<int>>ans;
	const int fromy = ps[now].first;
	const int fromx = ps[now].second;
	for (const  int  yrange : divs) {
		const int xrange = nums[now] / yrange;
		int left = max(0, fromx - xrange + 1);
		int right = min(X-xrange, fromx);
		int up = max(0, fromy - yrange + 1);
		int down = min(Y- yrange, fromy);
		for (int leftside = left; leftside <= right; ++leftside) {
			for (int upside = up; upside <= down; ++upside) {
				bool ok = true;
				for (int x = leftside; x < leftside+xrange; ++x) {
					for (int y = upside; y < upside+yrange; ++y) {
						if (field[y][x] && field[y][x] != now) {
							ok = false;
							break;
						}
					}
				}
				if (ok) {
					vector<vector<int>>nfield(field);
					for (int x = leftside; x < leftside + xrange; ++x) {
						for (int y = upside; y < upside + yrange; ++y) {
							nfield[y][x] = now;
						}
					}
					vector<vector<int>>nans(getans(nfield, now + 1, nums, ps));
					if (!ans.empty()) {
						if (!nans.empty())return vector<vector<int>>();
					}
					else {
						ans = nans;
					}
				}
			}
		}
		
	}
	return ans;
}
int main() {
	while (1) { cin >> X >> Y >> n;
	if (!X)break;
		vector<int>nums(n+1);
		for (int i = 0; i < n; ++i) {
			int x, y; cin >> x >> y;
			nums[x] = y;
		}
		vector<vector<int>>field(Y, vector<int>(X));
		vector<pair<int, int>>froms(n + 1);
		for (int y = 0; y < Y; ++y) {
			for (int x = 0; x < X; ++x) {
				int a; cin >> a;
				field[y][x] = a;
				if (a) {
					froms[a] = make_pair(y, x);
				}
			}
		}
		auto v=getans(field, 1, nums, froms);
		if (v.empty()) {
			cout << "NA" << endl;
		}
		else {
			for (int i = 0; i < Y; ++i) {
				for (int j = 0; j < X; ++j) {
					cout << v[i][j];
					if (j != X - 1)cout << " ";
				}
				cout << endl;
			}
		}
	}
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3032
final_rank: 10

code_snippet: #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(a) (a).begin(),(a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const int INF=1e9;
int cnt;
int ans[10][10];
int fld[10][10];
int X,Y;
int B[15],K[15],Bx[15],By[15],N;
vector<int> V[15];
bool pos(int x,int y){
      return 0<=x&&x<X&&0<=y&&y<Y;
}
bool ok(int x,int y,int w,int h,int n){
      REP(i,w)REP(j,h)if(!pos(x+i,y+j)||(fld[x+i][y+j]!=0&&fld[x+i][y+j]!=n)){
            //cout<<i<<" "<<j<<"no"<<endl;
            return false;
      }
      return true;
}
void rec(int n){
      if(n==N){
            REP(i,X)REP(j,Y)ans[i][j]=fld[i][j];
            cnt++;
            return;
      }
      int sx,sy;
      //cout<<n<<" "<<B[n]<<endl;
      //REP(i,Y)REP(j,X)printf("%d%c",fld[j][i],j==X-1?'\n':' ');
      REP(i,X)REP(j,Y)if(fld[i][j]==B[n]){sx=i;sy=j;};
      //cout<<sx<<" "<<sy<<endl;
      REP(i,V[n].size()){
            //cout<<V[n][i]<<endl;
            for(int x=sx-V[n][i]+1;x<=sx;x++){
                  for(int y=sy-K[n]/V[n][i]+1;y<=sy;y++){
                        if(ok(x,y,V[n][i],K[n]/V[n][i],B[n])){
                              REP(j,V[n][i])REP(k,K[n]/V[n][i])fld[x+j][y+k]=B[n];
                              rec(n+1);
                              REP(j,V[n][i])REP(k,K[n]/V[n][i]){
                                    if(x+j==Bx[n]&&y+k==By[n])continue;
                                    else fld[x+j][y+k]=0;
                              }
                        }
                  }
            }
      }
      return;
}
int main(){
      while(cin>>X>>Y>>N&&(X||Y||N)){
            REP(i,15)V[i].clear();
            cnt=0;
            REP(i,N){
                  cin>>B[i]>>K[i];
                  for(int j=1;j*j<=K[i];j++){
                        if(K[i]%j==0){
                              V[i].PB(j);
                              if(K[i]/j!=j)V[i].PB(K[i]/j);
                        }
                  }
            }
            REP(i,Y)REP(j,X){
                  cin>>fld[j][i];
                  if(fld[j][i]!=0){
                        REP(k,N)if(B[k]==fld[j][i]){
                              Bx[k]=j;By[k]=i;
                        }
                  }
            }
            rec(0);
            if(cnt==1){
                  REP(i,Y)REP(j,X)printf("%d%c",ans[j][i],j==X-1?'\n':' ');
            }else{
                  cout<<"NA"<<endl;
            }
      }
}
accuracy: 1
time_ms: 50
memory_kb: 1236
final_rank: 5

code_snippet: #include <iostream>
#include <vector>
#include <string>
#include <utility>
using namespace std;

#define fi first
#define se second
typedef pair<int,int> pii;

int X, Y, n;
vector<int> v;
vector<vector<int> > board;
vector<vector<int> > ans;
vector<vector<int> > out;
int ok[10][10][10][10];
bool use[16];

int solve(int y, int x) {
	int res = 0;
	if(y >= Y) {
		out = ans;
		return 1;
	}
	if(ans[y][x]) {
		return solve(y+(x+1)/X, (x+1)%X);
	}
	vector<vector<int> > cpy = ans;
	for(int i = y; i < Y; i++) {
		for(int j = x; j < X; j++) {
			if(ok[y][x][i][j] && !use[ok[y][x][i][j]]) {
				for(int k = y; k <= i; k++)
					for(int l = x; l <= j; l++)
						ans[k][l] = ok[y][x][i][j];
				use[ok[y][x][i][j]] = true;
				res += solve(y+(x+1)/X, (x+1)%X);
				ans = cpy;
				use[ok[y][x][i][j]] = false;
			}
		}
	}
	return res;
}

int main() {
	while(cin >> X >> Y >> n, X || Y || n) {
		v.clear(); board.clear(); ans.clear();
		out.clear();
		v.resize(n+1);
		board.resize(Y);
		ans.resize(Y);
		for(int i = 0; i < 10*10*10*10; i++)
			ok[0][0][0][i] = 0;
		for(int i = 0; i < 16; i++)
			use[i] = false;
		for(int i = 1; i < v.size(); i++) v[i] = -1;
		for(int i = 0; i < n; i++) {
			int b, k;
			cin >> b >> k;
			v[b] = k;
		}
		for(int i = 1; i < v.size(); i++) {
			if(v[i] < 0) {
				cout << "NA" << endl;
				continue;
			}
		}
		for(int i = 0; i < Y; i++) {
			board[i].resize(X);
			ans[i].resize(X);
			for(int j = 0; j < X; j++) {
				cin >> board[i][j];
				ans[i][j] = 0;
			}
		}

		for(int i = 0; i < Y; i++) {
			for(int j = 0; j < X; j++) {
				for(int k = i; k < Y; k++) {
					for(int l = j; l < X; l++) {
						int cnt = 0;
						for(int m = 1; m < v.size(); m++) {
							if((k-i+1) * (l-j+1) == v[m]) {
								bool flg = true;
								for(int y = 0; y < Y && flg; y++) {
									for(int x = 0; x < X && flg; x++) {
										if(i <= y && y <= k && j <= x && x <= l) {
											if(board[y][x] != 0 && board[y][x] != m)
												flg = false;
										}
										else {
											if(board[y][x] == m)
												flg = false;
										}
									}
								}
								if(flg) {
									ok[i][j][k][l] = m;
									cnt++;
								}
							}
						}
						if(cnt != 1) ok[i][j][k][l] = 0;
					}
				}
			}
		}

		int res = 0;
		res = solve(0,0);
		int sum = 0;
		for(int i = 0; i < v.size(); i++) sum += v[i];
		bool flg = true;
		for(int i = 0; i < out.size(); i++)
			for(int j = 0; j < out[i].size(); j++)
				flg &= out[i][j] != 0;

		if(res == 1 && sum == X*Y && flg) {
			for(int i = 0; i < out.size(); i++) {
				cout << out[i][0];
				for(int j = 1; j < out[i].size(); j++) {
					cout << " " << out[i][j];
				}
				cout << endl;
			}
		}
		else {
			cout << "NA" << endl;
		}
	}
}
accuracy: 1
time_ms: 10
memory_kb: 1292
final_rank: 2

code_snippet: #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;
}
accuracy: 1
time_ms: 30
memory_kb: 1240
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;

int X, Y, n;
int area[16];
int used[10][10];
int ok[10][10];
int app[10][10];
int T[16];

int cnt;

bool check(int sy, int sx, int ey, int ex, int k)
{
	int t = T[k];
	for (int i = sy; i < ey; i++){
		for (int j = sx; j < ex; j++){
			if (used[i][j] != 0) return false;
			if (app[i][j] != 0 && app[i][j] != k) return false;
			if (app[i][j] == k) t--;
		}
	}
	return t == 0;
}

void Fill(int sy, int sx, int ey, int ex, int k)
{
	//if (k) printf("%d %d %d %d %d\n", k, sy, sx, ey, ex);
	
	for (int i = sy; i < ey; i++){
		for (int j = sx; j < ex; j++){
			used[i][j] = k;
		}
	}
}

void search(int k)
{
	if (k > n){
		cnt++;
		memcpy((int*)ok, (int*)used, sizeof(ok));
		
		/*for (int i = 0; i < Y; i++){
			for (int j = 0; j < X; j++){
				printf("%d ", ok[i][j]);
			}
			puts("");
		}*/
		
		return;
	}
	for (int i = 0; i < Y; i++){
		for (int j = 0; j < X; j++){
			for (int h = 1; i + h <= Y; h++){
				for (int w = 1; j + w <= X; w++){
					if (area[k] != h * w) continue;
					if (!check(i, j, i + h, j + w, k)) continue;
					Fill(i, j, i + h, j + w, k);
					search(k + 1);
					Fill(i, j, i + h, j + w, 0);
				}
			}
		}
	}
}

void solve()
{
	cnt = 0;
	search(1);
	if (cnt != 1) puts("NA");
	else {
		for (int i = 0; i < Y; i++){
			for (int j = 0; j < X; j++){
				if (j) printf(" ");
				printf("%d", ok[i][j]);
			}
			puts("");
		}
	}
}

int main()
{
	while (scanf("%d %d %d", &X, &Y, &n), X){
		memset(T, 0, sizeof(T));
		
		for (int i = 0; i < n; i++){
			int b, k;
			scanf("%d %d", &b, &k);
			area[b] = k;
		}
		for (int i = 0; i < Y; i++){
			for (int j = 0; j < X; j++){
				scanf("%d", &app[i][j]);
				T[app[i][j]]++;
			}
		}
		solve();
	}
}
accuracy: 1
time_ms: 410
memory_kb: 1200
final_rank: 18

code_snippet: #include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
//#include <utility>
//#include <memory>
//#include <functional>
//#include <deque>
//#include <cctype>
//#include <ctime>
//#include <numeric>
//#include <list>
//#include <iomanip>

//#if __cplusplus >= 201103L
//#include <array>
//#include <tuple>
//#include <initializer_list>
//#include <unordered_set>
//#include <unordered_map>
//#include <forward_list>
//
//#define cauto const auto&
//#else

//#endif

using namespace std;


namespace{
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}


#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;

VV(pii) v(101);
vvint vv;
int x,y,n;
vector<pair<int,pii>> in;
vvint ans;
int cnt;
void foo(int a){
	if(a==n){
		cnt++;
		ans=vv;
		return;
	}
	int yy=in[a].S.F;
	int xx=in[a].S.S;
	int tt=in[a].F;
	rep(i,v[tt].size()){
		int ty=v[tt][i].F;
		int tx=v[tt][i].S;
		reep(j,yy-ty+1,yy+1){
			reep(k,xx-tx+1,xx+1){
				if(j<0||j+ty-1>=y||k<0||k+tx-1>=x) continue;
				bool f=true;
				reep(jj,j,j+ty){
					reep(kk,k,k+tx){
						if(vv[jj][kk]!=0&&vv[jj][kk]!=a+1){
							f=false;
						}
					}
				}
				if(f){
					reep(jj,j,j+ty){
						reep(kk,k,k+tx){
							vv[jj][kk]=a+1;
						}
					}
					foo(a+1);
					reep(jj,j,j+ty){
						reep(kk,k,k+tx){
							vv[jj][kk]=0;
						}
					}
					vv[yy][xx]=a+1;
				}
			}
		}
	}
}
void mainmain(){
	reep(i,1,11){
		reep(j,1,11){
			v[i*j].PB(pii(i,j));
			v[i*j].PB(pii(j,i));
		}
	}
	rep(i,101){
		sort(ALL(v[i]));
		v[i].erase(unique(ALL(v[i])),v[i].end());
	}
	while(cin>>x>>y>>n,x||y||n){
		in=vector<pair<int,pii>>(n);
		initvv(vv,y,x);
		rep(i,n){
			int t;
			cin>>t;
			cin>>in[t-1].F;
		}
		rep(i,y){
			rep(j,x){
				cin>>vv[i][j];
				if(vv[i][j]!=0){
					in[vv[i][j]-1].S.F=i;
					in[vv[i][j]-1].S.S=j;
				}
			}
		}
		cnt=0;
		foo(0);
		if(cnt==1){
			rep(i,ans.size()){
				rep(j,ans[i].size()){
					if(j) cout<<" ";
					cout<<ans[i][j];
				}
				cout<<endl;
			}
			continue;
		}
		else{
			cout<<"NA"<<endl;
		}
	}
}



}
main() try{
    mainmain();
}
catch(...){
}
accuracy: 1
time_ms: 20
memory_kb: 1224
final_rank: 3

code_snippet: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
  
using namespace std;
  
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P> PP;
  
static const double EPS = 1e-8;

static const int tx[] = {0,1,0,-1};
static const int ty[] = {-1,0,1,0};

int stage_w,stage_h,total_buyers;
int ans[50][50];

void disp(int stage[50][50]){
  for(int y = 0; y < stage_h; y++){
    for(int x = 0; x < stage_w; x++){
      printf("%d%s",stage[y][x], x == stage_w - 1 ? "" : " ");
    }
    printf("\n");
  }
}

bool check(int stage[50][50]){
  for(int y = 0; y < stage_h; y++){
    for(int x = 0; x < stage_w; x++){
      if(stage[y][x] == 0) return false;
    }
  }
  memcpy(ans,stage,sizeof(int)*50*50);
  return true;
}

int dfs(int id,int areas[50],P occupied[50],int stage[50][50]){
  if(id > total_buyers){
    return check(stage) ? 1 : 0;
  }

  int res = 0;
  for(int w = 1; w <= areas[id]; w++){
    int h = areas[id] / w;
    if(h * w != areas[id]) continue;
    int cx = occupied[id].first;
    int cy = occupied[id].second;

    for(int lx = cx - w + 1; lx <= cx; lx++){
      if(lx < 0) continue;

      int rx = lx + w - 1;

      if(rx >= stage_w) continue;
      for(int ly = cy - h + 1; ly <= cy; ly++){
        if(ly < 0) continue;

        int ry = ly + h - 1;
        if(ry >= stage_h) continue;

        int prev[50][50];
        memcpy(prev,stage,sizeof(int)*50*50);
        for(int x = lx; x <= rx; x++){
          for(int y = ly; y <= ry; y++){
            if(stage[y][x] == 0){
              stage[y][x] = id;
            }
            else if(stage[y][x] != id){
              goto fail;
            }
          }
        }
        res += dfs(id + 1,areas,occupied,stage);
      fail:;
        memcpy(stage,prev,sizeof(int)*50*50);
      }
    }
  }

  return res;
}

int main(){

  while(~scanf("%d %d %d",&stage_w,&stage_h,&total_buyers)){
    if(stage_w == 0 && stage_h == 0) break;

    int stage[50][50];
    int areas[50];
    for(int buyer_i = 0; buyer_i < total_buyers; buyer_i++){
      int id;
      int area;
      scanf("%d %d",&id,&area);
      areas[id] = area;
    }

    P occupied[50];
    for(int y = 0; y < stage_h; y++){
      for(int x = 0; x < stage_w; x++){
        scanf("%d",&stage[y][x]);
        if(stage[y][x] != 0){
          occupied[stage[y][x]] = P(x,y);
        }
      }
    }

    if(dfs(1,areas,occupied,stage) == 1){
      disp(ans);
    }
    else{
      printf("NA\n");
    }
  }
}
accuracy: 1
time_ms: 340
memory_kb: 1372
final_rank: 17

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

int h,w,n;
map<int,int> ma;
map<int,pair<int,int> > p;
vector<vector<int> > v;

bool can(int i,int j,int ny,int nx)
{
	if(0 <= i && ny <= h && 0 <= j && nx <= w)
	{
		return true;
	}
	else return false;
}

bool canSet(int num,int y,int x,int y2,int x2)
{
	bool flag = true;

	REP(i,y,y2)
	{
		REP(j,x,x2)
		{
			if(v[i][j] != 0)
			{ 
				flag = false;
			}
		}
	}

	return flag;
}

void set(int num,int y,int x,int y2,int x2)
{
	REP(i,y,y2)
	{
		REP(j,x,x2)
		{
			v[i][j] = num;
		}
	}
}

int cnt = 0;
vector<vector<int> > v2;

void dfs(int num)
{
	if(num == 0)
	{
		if(cnt == 0)
		{
			bool flag = true;
			rep(i,h)
			{
				rep(j,w)
				{
					if(v[i][j] == 0) flag = false;
				}
			}

			if(flag)
			{
				v2.resize(h);
				rep(i,h) v2[i].resize(w);
				rep(i,h) rep(j,w) v2[i][j] = v[i][j];
			
				cnt++;
			}
		}
		else
		{
			bool flag = true;

			rep(i,h)
			{
				rep(j,w)
				{
					if(v[i][j] == 0) flag = false;
				}
			}

			if(flag)
			{
				cnt++;
			}
		}
	}
	else
	{
		REP(y,1,ma[num]+1)
		{
			if(ma[num]%y == 0)
			{
				int x = ma[num]/y;
				REP(i,p[num].first-y+1,p[num].first+1)
				{
					REP(j,p[num].second-x+1,p[num].second+1)
					{
						int ny = i+y;
						int nx = j+x;

						if(can(i,j,ny,nx))
						{
							v[p[num].first][p[num].second] = 0;
							if(canSet(num,i,j,ny,nx))
							{
								set(num,i,j,ny,nx);
								dfs(num-1);
								set(0,i,j,ny,nx);
							}
							v[p[num].first][p[num].second] = num;
						}
					}
				}
			}
		}
	}
}

int main()
{
	while(cin >> w >> h >> n)
	{
		if(h == 0 && w == 0 && n == 0) break;

		v.resize(h);
		rep(i,h) v[i].clear();

		ma.clear();
		p.clear();
		cnt = 0;

		rep(i,n)
		{
			int a,b;
			cin >> a >> b;
			ma[a] = b;
		}

		v.resize(n);
		rep(i,h)
		{
			rep(j,w)
			{
				int temp;
				cin >> temp;
				v[i].push_back(temp);

				if(1 <= temp && temp <= 15)
				{
					p[temp] = pair<int,int>(i,j);
				}
			}
		}

		dfs(n);

		if(cnt == 1)
		{
			rep(i,h)
			{
				rep(j,w)
				{
					cout << v2[i][j];
					if(j != w-1) cout << " ";
				}
				cout << endl;
			}
		}
		else
		{
			cout << "NA" << endl;
		}

	}
	return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 1232
final_rank: 6

code_snippet: #include <iostream>
#include <vector>
#include <cstring>
#include <map>
using namespace std;

int X, Y, n;
int id[20], k[20], sx[20], sy[20];
int bd[10][10], ans[10][10];
vector<pair<int,int> > area[101];

int dfs(int u) {
	if (u == n) {
		memcpy(ans, bd, sizeof ans);
		return 1;
	}
	int h, w, tx, ty, lx, rx, uy, dy;
	tx = sx[id[u]];
	ty = sy[id[u]];
	bd[ty][tx] = 0;
	int ret = 0;
	for (int i=0; i<(int)area[k[u]].size(); ++i) {
		h = area[k[u]][i].first;
		w = area[k[u]][i].second;
		lx = max(tx-w+1, 0);
		rx = min(tx+w-1, X-1);
		uy = max(ty-h+1, 0);
		dy = min(ty+h-1, Y-1);
		if (rx-lx+1 < w || dy-uy+1 < h) continue;
		for (int y=uy; y+h-1<=dy; ++y) {
			for (int x=lx; x+w-1<=rx; ++x) {
				bool ok = true;
				for (int y2=y; y2<y+h && ok; ++y2) {
					for (int x2=x; x2<x+w; ++x2) {
						if (bd[y2][x2] != 0) {
							ok = false;
							break;
						}
					}
				}
				if ( ! ok) continue;
				for (int y2=y; y2<y+h; ++y2) {
					for (int x2=x; x2<x+w; ++x2) {
						bd[y2][x2] = id[u];
					}
				}
				ret += dfs(u+1);
				for (int y2=y; y2<y+h; ++y2) {
					for (int x2=x; x2<x+w; ++x2) {
						bd[y2][x2] = 0;
					}
				}
				if (2 <= ret) return 2;
			}
		}
	}
	bd[ty][tx] = id[u];
	return ret;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	for (int i=1; i<=10; ++i) {
		for (int j=1; j<=10; ++j) {
			area[i*j].push_back(make_pair(i, j));
		}
	}
	while (cin >> X >> Y >> n, X) {
		int sum = 0;
		for (int i=0; i<n; ++i) {
			cin >> id[i] >> k[i];
			sum += k[i];
		}
		for (int i=0; i<Y; ++i) {
			for (int j=0; j<X; ++j) {
				cin >> bd[i][j];
				if (bd[i][j] != 0) {
					sx[bd[i][j]] = j;
					sy[bd[i][j]] = i;
				}
			}
		}
		int res = dfs(0);
		if (X * Y != sum || res == 0 || 2 <= res) {
			cout << "NA\n";
			continue;
		}
		for (int i=0; i<Y; ++i) {
			for (int j=0; j<X; ++j) {
				cout << (j == 0 ? "" : " ") << ans[i][j];
			}
			cout << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1208
final_rank: 1