标签:dfs rap 节点 iostream char out turn cstring fine
没什么意思,注意下细节
//#include<bits/stdc++.h> #include<iostream> #include<cstdio> #include<cstring> #define inf (0x3f3f3f3f) using namespace std; const int maxn = 16; char Grape[maxn][maxn]; bool vis[maxn]; int n,k,ans; void dfs(int row,int num) { if(num==k) { ++ans; return; } if(row>n) return; for(int i=1;i<=n;++i) { if(Grape[row][i]==‘#‘&&!vis[i]) {//对于每一个地图的未访问过节点 vis[i] = true; dfs(row+1,num+1); vis[i] = false; } } dfs(row+1,num);//(SB了,不放旗子时是不需要有位置可以的)拿出循环外,找了n久,一直TLE } int main() { ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); while(cin>>n>>k) { if(n==-1) break; for(int i=1;i<=n;++i) for(int j=1;j<=n;++j) cin>>Grape[i][j]; memset(vis,false,sizeof(vis));//每一列 ans = 0; dfs(1,0); cout<<ans<<‘\n‘; } }
标签:dfs rap 节点 iostream char out turn cstring fine
原文地址:https://www.cnblogs.com/newstartCY/p/11620201.html