2012杭州现场赛
2:08:19
5:00:00
#include<iostream>
#include<stack>
#include<cstring>
#include<map>
#include<string>
#include<queue>
#include<algorithm>
#include<cstdio>
#include<utility>
using namespace std;
struct node {
int x, y, value;
}p[15];
int steparr[4][2] = { -1,0,1,0,0,-1,0,1 };
int vis[55][55],vis1[55][55];
int n;
queue<node>q;
bool bfs() {
while (!q.empty()) {
node now = q.front();
q.pop();
if (!now.value)
continue;
for (int i = 0; i < 4; i++) {
int nowx = now.x + steparr[i][0];
int nowy = now.y + steparr[i][1];
if (nowx <= 0 || nowx > n || nowy <= 0 || nowy > n || vis[nowx][nowy] >= now.value - 1)
continue;
vis[nowx][nowy] = now.value - 1;
q.push(node{ nowx,nowy ,vis[nowx][nowy] });
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (vis[i][j] == -1&&!vis1[i][j]) {
return false;
}
}
}
return true;
}
int main() {
while (scanf("%d", &n)) {
if (!n)
break;
int k;
scanf("%d", &k);
memset(vis1, 0, sizeof(vis1));
for (int i = 0; i < k; i++) {
scanf("%d%d", &p[i].x, &p[i].y);
vis1[p[i].x][p[i].y] = 1;
}
for (int i = 0; i < k; i++) {
scanf("%d", &p[i].value);
}
int max = 15;
for (int i = 0; i < (1 << k); i++) {
memset(vis, -1, sizeof(vis));
for (int j = 0; j < k; j++) {
if (i&(1 << j)) {
q.push(p[j]);
vis[p[j].x][p[j].y] = p[j].value;
}
}
int now = q.size();
if (bfs() && now<max) {
max = now;
}
}
if (max == 15) {
printf("-1\n");
}
else {
printf("%d\n", max);
}
}
}