ソースコード
#include <cstdio>
#include <cstdlib>
int scN,scM;
int scB[2][10];
clock_t scStartTime,scEndTime;
void scInput(){
int i;
scanf("%d%d",&scN,&scM);
for(i=0;i<scM;++i){scanf("%d%d",&scB[0][i],&scB[1][i]);}
scStartTime=clock();
}
void scOutput(int s){
scEndTime=clock();
printf("Ans= %d, time=%d\n",s,(int)(scEndTime-scStartTime));
}
using namespace std;
int block[23][23];
int left(int, int, int);
int right(int, int, int);
int down(int, int, int);
int up(int, int, int);
int count(int move, int x, int y){
int cnt = 0;
if(!block[11 + x + 1][11 + y ]) cnt += right(move - 1, x + 1, y );
if(!block[11 + x ][11 + y + 1]) cnt += up (move - 1, x , y + 1);
if(!block[11 + x - 1][11 + y ]) cnt += left (move - 1, x - 1, y );
if(!block[11 + x ][11 + y - 1]) cnt += down (move - 1, x , y - 1);
return cnt;
}
int left(int move, int x, int y){
if(!x && !y) return move ? 0 : 1;
int cnt = 0;
block[11 + x][11 + y] = 1;
if(!block[11 + x ][11 + y + 1] && abs(x ) + abs(y + 1) < move) cnt += up (move - 1, x , y + 1);
if(!block[11 + x - 1][11 + y ] && abs(x - 1) + abs(y ) < move) cnt += left (move - 1, x - 1, y );
if(!block[11 + x ][11 + y - 1] && abs(x ) + abs(y - 1) < move) cnt += down (move - 1, x , y - 1);
block[11 + x][11 + y] = 0;
return cnt;
}
int right(int move, int x, int y){
if(!x && !y) return move ? 0 : 1;
int cnt = 0;
block[11 + x][11 + y] = 1;
if(!block[11 + x + 1][11 + y ] && abs(x + 1) + abs(y ) < move) cnt += right(move - 1, x + 1, y );
if(!block[11 + x ][11 + y + 1] && abs(x ) + abs(y + 1) < move) cnt += up (move - 1, x , y + 1);
if(!block[11 + x ][11 + y - 1] && abs(x ) + abs(y - 1) < move) cnt += down (move - 1, x , y - 1);
block[11 + x][11 + y] = 0;
return cnt;
}
int down(int move, int x, int y){
if(!x && !y) return move ? 0 : 1;
int cnt = 0;
block[11 + x][11 + y] = 1;
if(!block[11 + x + 1][11 + y ] && abs(x + 1) + abs(y ) < move) cnt += right(move - 1, x + 1, y );
if(!block[11 + x - 1][11 + y ] && abs(x - 1) + abs(y ) < move) cnt += left (move - 1, x - 1, y );
if(!block[11 + x ][11 + y - 1] && abs(x ) + abs(y - 1) < move) cnt += down (move - 1, x , y - 1);
block[11 + x][11 + y] = 0;
return cnt;
}
int up(int move, int x, int y){
if(!x && !y) return move ? 0 : 1;
int cnt = 0;
block[11 + x][11 + y] = 1;
if(!block[11 + x + 1][11 + y ] && abs(x + 1) + abs(y ) < move) cnt += right(move - 1, x + 1, y );
if(!block[11 + x ][11 + y + 1] && abs(x ) + abs(y + 1) < move) cnt += up (move - 1, x , y + 1);
if(!block[11 + x - 1][11 + y ] && abs(x - 1) + abs(y ) < move) cnt += left (move - 1, x - 1, y );
block[11 + x][11 + y] = 0;
return cnt;
}
int main(){
scInput();
for(int i = 0;i < scM;i++) if(abs(scB[0][i]) + abs(scB[1][i]) <= 11) block[11 + scB[0][i]][11 + scB[1][i]] = 1;
scOutput(count(scN, 0, 0));
return 0;
}