https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=24&problem=3057
注意事項:
騎士從(0,0)開始走。 每到他可以走到的點 ,他會計算說,有多少個點可以走到這裡。 記得,他可以走到(0,0) 。 輸出有多少 偶數 & 奇數。
於是乎,debug好久,終於AC了
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <queue>
#include <utility>
using namespace std;
#define S second
#define F first
#define MP make_pair
typedef pair<int,int> pii;
const int MAX_N = 203;
int mp[MAX_N][MAX_N];
int can[MAX_N][MAX_N];
int f(int x,int y,int n,int m) {
if (mp[x][y] == 0) return 0;
int dx[8] = {n,n,-n,-n,m,m,-m,-m},dy[8] = {m,-m,m,-m,n,-n,n,-n};
int ret=0;
for (int i=0;8>i;i++) {
//1-->okay
int tx=x + dx[i],ty=y+dy[i];
if (0<=tx && 0<=ty && mp[tx][ty] == 1) ret++;
}
return (n==m || n==0 || m==0 ? ret/2 : ret);
}
int main () {
// freopen("output.txt","w",stdout);
int T;
scanf("%d",&T);
for (int qq=1;T>=qq;qq++) {
printf("Case %d: ",qq);
int a,b,n,m;
scanf("%d %d %d %d",&a,&b,&n,&m);
memset(mp,0,sizeof(mp));
memset(can,0,sizeof(can));
can[0][0] = 1;
for (int x=0;a>x;x++) {
for (int y=0;b>y;y++) {
mp[x][y]= 1;
}
}
int q;
scanf("%d",&q);
while (q--) {
int x,y;
scanf("%d %d",&x,&y);
mp[x][y] = 0;
}
int even=0,odd=0;
queue<pii> que;
que.push(MP(0,0));
while (!que.empty()) {
pii tmp = que.front();
que.pop();
int t=f(tmp.F,tmp.S,n,m);
if (t!=0 && t%2==0 || t==0 && tmp == MP(0,0)) even++;
else if (t%2==1) odd++;
int dx[8] = {n,n,-n,-n,m,m,-m,-m},dy[8] = {m,-m,m,-m,n,-n,n,-n};
int x=tmp.F, y=tmp.S;
// cout<<endl<<x<<" "<<y<<endl;
for (int i=0;8>i;i++) {
int tx=x + dx[i],ty=y+dy[i];
if (0<=tx && 0<=ty && mp[tx][ty] == 1 && can[tx][ty] == 0) {
que.push(MP(tx,ty));
can[tx][ty] = 1;
}
}
}
printf("%d %d\n",even,odd);
}
}
/*
1
11 53 0 7
4
4 12
0 18
7 46
1 46
*/
沒有留言:
張貼留言