Philosopher’s Walk --DFS

题意:

Philosopher’s Walk 图,告诉你step返回位置。

思路:

按四个块DFS

  1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>//freopen("C:\Users\13606\Desktop\Input.txt","r",stdin);
  6 #include <bitset>
  7 //#include <map>
  8 //#include<unordered_map>
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr
 13 #include <string>
 14 #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 #include <cassert>
 21 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 22 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
 23 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 24 //******************
 25 int abss(int a);
 26 int lowbit(int n);
 27 int Del_bit_1(int n);
 28 int maxx(int a,int b);
 29 int minn(int a,int b);
 30 double fabss(double a);
 31 void swapp(int &a,int &b);
 32 clock_t __STRAT,__END;
 33 double __TOTALTIME;
 34 void _MS(){__STRAT=clock();}
 35 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__STRAT)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
 36 //***********************
 37 #define rint register int
 38 #define fo(a,b,c) for(rint a=b;a<=c;++a)
 39 #define fr(a,b,c) for(rint a=b;a>=c;--a)
 40 #define mem(a,b) memset(a,b,sizeof(a))
 41 #define pr printf
 42 #define sc scanf
 43 #define ls rt<<1
 44 #define rs rt<<1|1
 45 typedef vector<int> VI;
 46 typedef long long ll;
 47 const double E=2.718281828;
 48 const double PI=acos(-1.0);
 49 //const ll INF=(1LL<<60);
 50 const int inf=(1<<30);
 51 const double ESP=1e-9;
 52 const int mod=(int)1e9+7;
 53 const int N=(int)1e6+10;
 54 
 55 struct node
 56 {
 57     int x,y;
 58 };
 59 node dfs(int n,int step)
 60 {
 61     if(n==1)return {1,1};
 62     int block=n/2;
 63     block*=block;
 64     if(step>=1&&step<=block)            return {dfs(n/2,block+1-step).y,n/2+1-dfs(n/2,block+1-step).x};
 65     else if(step>block&&step<=2*block)    return {dfs(n/2,step-block).x,n/2+dfs(n/2,step-block).y};
 66     else if(step>block*2&&step<=3*block)return {n/2+dfs(n/2,step-block*2).x,n/2+dfs(n/2,step-block*2).y};
 67     else                                 return {n+1-dfs(n/2,4*block+1-step).y,dfs(n/2,4*block+1-step).x};
 68 }
 69 
 70 int main()
 71 {
 72     int n,step;
 73     sc("%d%d",&n,&step);
 74     node ans=dfs(n,step);
 75     pr("%d %d
",ans.x,ans.y);
 76     return 0;
 77 }
 78 
 79 /**************************************************************************************/
 80 
 81 int maxx(int a,int b)
 82 {
 83     return a>b?a:b;
 84 }
 85 
 86 void swapp(int &a,int &b)
 87 {
 88     a^=b^=a^=b;
 89 }
 90 
 91 int lowbit(int n)
 92 {
 93     return n&(-n);
 94 }
 95 
 96 int Del_bit_1(int n)
 97 {
 98     return n&(n-1);
 99 }
100 
101 int abss(int a)
102 {
103     return a>0?a:-a;
104 }
105 
106 double fabss(double a)
107 {
108     return a>0?a:-a;
109 }
110 
111 int minn(int a,int b)
112 {
113     return a<b?a:b;
114 }
原文地址:https://www.cnblogs.com/--HPY-7m/p/11769860.html