蓝桥杯 (计算路径)
题意:7*7的矩阵,出发点和终点都是0,0 ,问有多少条路径,步数不超过12步。
考虑的边界值不仅仅是迷宫边界,还有最少步数,所有的限制条件都要考虑
#include <iostream> #include <algorithm> #include <cstdio> #include <string> #include <cstring> #include <cstdlib> #include <map> #include <vector> #include <set> #include <queue> #include <stack> #include <cmath> using namespace std; #define lli long long #define pq priority_queue<int> #define pql priority_queue<ll> #define pqn priority_queue<node> #define v vector<int> #define vl vector<ll> #define read(x) scanf("%d",&x) #define read2(x,y) scanf("%d %d",&x,&y); #define lread(x) scanf("%lld",&x); #define pt(x) printf("%d\n",(x)) #define yes printf("YES\n"); #define no printf("NO\n"); #define gcd __gcd #define out(x) cout<<x<<endl; #define rep(j,k) for (int i = (int)(j); i <= (int)(k); i++) #define input(k) for (int i = 1; i <= (int)(k); i++) {scanf("%d",&a[i]) ; } #define mem(s,t) memset(s,t,sizeof(s)) #define ok return 0; #define TLE std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define test cout<<" ++++++ "<<endl; //二叉树 #define lson rt<<1, l, m #define rson rt<<1|1, m+1, r //线段树 #define ls now<<1 #define rs now<<1|1 const int inf = 1e6+5; int vis[8][8],cnt=0,ans; int dir[4][2] = {1,0,-1,0,0,1,0,-1}; void DFS(int x,int y) { if(ans>12) return ; if(x==1 && y==1 && ans>2) {cnt++;return ;} for(int i=0;i<4;i++) { int nx=x+dir[i][0]; int ny=y+dir[i][1]; if(nx<1 || ny<1 || nx > 7 || ny>7 ) continue; if(!vis[nx][ny] ) { vis[nx][ny]=1; ans++; DFS(nx,ny); ans--; vis[nx][ny]=0; } } } int main() { mem(vis,0); ans=0; DFS(1,1); cout<<cnt<<endl; }
所遇皆星河