Topic links
Test Instructions A cool traveler is ready to measure the city in his own way. From 0 to 0, take the first step, step 1. Step is 2 at the second step, and the next step is to turn the 90° direction without going through the step. and avoid roadblocks. Then output all scenarios that can go back to 0,0 and the number of scenarios
analysis DFS pruning
#include <bits/stdc++.h> using namespace std;
const int v=233;
int N,ans;
int dx[4]={1,0,0,-1},dy[4]={0,1,-1,-0};
int buf[1000][1000];
Char ch[4]={' e ', ' n ', ' s ', ' W '},arr[1001];
int judge (int a,int b) {//Ensure each 90° turn, if (a==0 | | a==3) return b!=0 && b!=3;
if (a==1 | | a==2) return b!=1 && b!=2;
return 1;
} bool Safe (int x,int y,int a,int b,int f) {//Make sure there are no roadblocks on the road; while (X!=a | | y!=b) {X+=DX[F];Y+=DY[F];
if (Buf[x+v][y+v] >0) return false;
} return buf[x+v][y+v]==0;
} void DFS (int step,int x,int y,int f) {if (step>n) {if (!x &&!y) {ans++;p uts (arr);}
Return } for (int i=0;i<4;i++) {if (Judge (f,i) && Safe (x,y,x+step*dx[i],y+step*dy[i],i)) {arr[st
Ep-1]=ch[i];
Buf[x+step*dx[i]+v][y+step*dy[i]+v]=-1;
DFS (Step+1,x+step*dx[i],y+step*dy[i],i);
buf[x+step*dx[i]+v][y+step*dy[i]+v]=0;
}}} int main (void) {int t,m,x,y; cin>≫t;
while (t--&& cin>>n>>m) {ans=0;
memset (buf,0,sizeof (BUF));
memset (arr,0,sizeof (arr));
while (m--) {cin>>x>>y; buf[x+v][y+v]=1;}
if (n==7 | | n==8 | | n==15 | | n==16) {//Here is a reference to the Super handsome J1nab1n9 pruning way DFS (1,0,0,5);
} printf ("Found%d Golygon (s). \ n", ans);
} return 0; }
j1nab1n9 Blog Portal ->>j1nab1n9
Well, the smart j1nab1n9 thought that if the route could be pieced together into a rectangle, That is, the sum of the steps can be divisible by 4. Then n, which satisfies this condition, is likely to go back to the origin.