#include<cstring>
using namespace std;voidfre(){freopen("A.txt","r",stdin);freopen("Ans.txt","w",stdout);}voidFre(){freopen("A.txt","r",stdin);}#define ios ios::sync_with_stdio(false)#define Pi acos(-1)#define pb push_back#define fi first#define se second#define ll long long#define ull unsigned long long #define db double#define Pir pair<int, int>#define PIR pair<Pir, Pir>#define INF 0x3f3f3f3f#define mod (ll)(1e9 + 7)#include<iostream>#include<algorithm>
using namespace std;constint mxn =10;int n, k;char mz[mxn][mxn];int lin[mxn], row[mxn];intdfs(int ct,int l){if(ct == k)return1;int sm =0;for(int i = l; i <= n; i ++){if(row[i])continue;for(int j =1; j <= n; j ++){if(lin[j])continue;if(mz[i][j]=='.')continue;
row[i]=1, lin[j]=1;
sm +=dfs(ct +1, i +1);
row[i]=0, lin[j]=0;}}return sm;}intmain(){/* fre(); */while(scanf("%d %d",&n,&k)&& n !=-1){memset(lin,0,sizeof(lin));memset(row,0,sizeof(row));for(int i =1; i <= n; i ++)scanf("%s", mz[i]+1);printf("%d\n",dfs(0,1));}return0;}
B - Dungeon Master POJ - 2251
思路
二维迷宫问题变形。。。
代码
#include<iostream>#include<algorithm>#include<cstring>#include<queue>voidfre(){freopen("A.txt","r",stdin);freopen("Ans.txt","w",stdout);}voidFre(){freopen("A.txt","r",stdin);}#define ios ios::sync_with_stdio(false)#define Pi acos(-1)#define pb push_back#define fi first#define se second#define ll long long#define ull unsigned long long #define db double#define Pir pair<int, int>#define PIR pair<Pir, Pir>#define INF 0x3f3f3f3f#define mod (ll)(1e9 + 7)
using namespace std;constint mxn =35;int n, k;char mz[mxn][mxn][mxn];int mk[mxn][mxn][mxn];int x, y, z;struct Node
{int x, y, z, t;
bool operator ==(const Node T)const{if(T.x == x && T.y == y && T.z == z)return true;return false;}} S, E;
Node mov[6]={{1,0,0},{0,1,0},{0,0,1},{-1,0,0},{0,-1,0},{0,0,-1}};
bool good(Node p){if(p.x >=1&& p.y >=1&& p.z >=1&& p.x <= x && p.y <= y && p.z <= z && mz[p.z][p.x][p.y]!='#'&&! mk[p.z][p.x][p.y])return true;return false;}int tim;
bool dfs(Node P,int t){int fg =0;if(P == E){
tim =min(tim, t);
fg =1;}for(int i =0; i <6; i ++){
Node now ={ P.x + mov[i].x, P.y + mov[i].y, P.z + mov[i].z };if(!good(now))continue;
mk[now.z][now.x][now.y]=1;if(dfs(now, t +1)) fg =1;
mk[now.z][now.x][now.y]=0;}if(fg)return true;return false;}int sp = INF;
bool bfs(){
queue<Node> q;
S.t =0;
q.push(S);while(! q.empty()){
S = q.front(); q.pop();if(S == E){
sp = S.t;return true;}for(int i =0; i <6; i ++){
Node now ={ S.x + mov[i].x, S.y + mov[i].y, S.z + mov[i].z, S.t +1};if(!good(now))continue;
mk[now.z][now.x][now.y]=1;
q.push(now);}}return false;}intmain(){/* fre(); */while(scanf("%d %d %d",&z,&x,&y)&& x){memset(mk,0,sizeof(mk));
tim = INF;
sp = INF;for(int i =1; i <= z; i ++){for(int j =1; j <= x; j ++){scanf("%s", mz[i][j]+1);for(int k =1; k <= y; k ++){if(mz[i][j][k]=='S')
S.z = i, S.x = j, S.y = k;if(mz[i][j][k]=='E')
E.z = i, E.x = j, E.y = k;}}}
mk[S.z][S.x][S.y]=1;if(bfs())printf("Escaped in %d minute(s).\n", sp);elseprintf("Trapped!\n");}return0;}
#include<iostream>#include<algorithm>#include<cstring>#include<queue>#include<cstdio>voidfre(){freopen("A.txt","r",stdin);freopen("Ans.txt","w",stdout);}voidFre(){freopen("A.txt","r",stdin);}#define ios ios::sync_with_stdio(false)#define Pi acos(-1)#define pb push_back#define fi first#define se second#define ll long long#define ull unsigned long long #define db double#define Pir pair<int, int>#define PIR pair<Pir, Pir>#define INF 0x3f3f3f3f#define mod (ll)(1e9 + 7)
using namespace std;constint mxn =20;int n, m;int ar[mxn][mxn], br[mxn][mxn];int op[mxn][mxn], ans[mxn][mxn];voidturn(int x,int y){
br[x][y]^=1;
br[x -1][y]^=1;
br[x +1][y]^=1;
br[x][y -1]^=1;
br[x][y +1]^=1;}
bool Solve(int& sm){memcpy(br, ar,sizeof(ar));for(int i =1; i <= m; i ++)//反转第一行if(op[1][i])turn(1, i);for(int i =2; i <= n; i ++)//反转其它行for(int j =1; j <= m; j ++){if(br[i -1][j]){turn(i, j);
op[i][j]=1;
sm ++;}}for(int i =1; i <= m; i ++)if(br[n][i])return false;return true;}intmain(){/* fre(); */scanf("%d %d",&n,&m);for(int i =1; i <= n; i ++)for(int j =1; j <= m; j ++)scanf("%d",&ar[i][j]);int sum = INF;for(int i =0; i <(1<< m +1); i ++)//二进制枚举第一行的每个位置的是否反转的情况2^15 {memset(op,0,sizeof(op));int sm =0;//总共的反转次数for(int j =1, t = i; j <= m; j ++)//第一行反转情况进行赋值{
op[1][j]=(t &1);
sm += op[1][j];
t >>=1;}if(Solve(sm)&& sm < sum){
sum = sm;memcpy(ans, op,sizeof(op));}}if(sum == INF)printf("IMPOSSIBLE\n"),exit(0);for(int i =1; i <= n; i ++){for(int j =1; j <= m; j ++)printf("%d ", ans[i][j]);printf("\n");}return0;}
E - Find The Multiple POJ - 1426
思路
这题怎么说呢,对于当前题目的给定的数据范围,我们可以知道 我所要凑出来的0、1组成的数字是不会超过 long long 数据范围的,所以可以直接用dfs暴力模拟过程求解,
但是当数据范围的 在扩大的时候,,,这时候 就要对dfs进行优化的了(用 同余定理 什么的)
代码
#include<iostream>#include<algorithm>#include<cstring>#include<queue>#include<cstdio>voidfre(){freopen("A.txt","r",stdin);freopen("Ans.txt","w",stdout);}voidFre(){freopen("A.txt","r",stdin);}#define ios ios::sync_with_stdio(false)#define Pi acos(-1)#define pb push_back#define fi first#define se second#define ll long long#define ull unsigned long long #define db double#define Pir pair<int, int>#define PIR pair<Pir, Pir>#define INF 0x3f3f3f3f#define mod (ll)(1e9 + 7)
using namespace std;constint mxn =20;int n, fg;voiddfs(int cur, ll num)//cur 表示是十进制是几位、num表示当前 枚举的数{if(cur ==19|| fg)return;if(num % n ==0){printf("%lld\n", num);
fg =1;return;}dfs(cur +1, num *10);dfs(cur +1, num *10+1);}intmain(){/* fre(); */while(scanf("%d",&n)&& n){
fg =0;dfs(0,1);}return0;}
#include<iostream>#include<algorithm>#include<cstring>#include<queue>#include<cstdio>voidfre(){freopen("A.txt","r",stdin);freopen("Ans.txt","w",stdout);}voidFre(){freopen("A.txt","r",stdin);}#define ios ios::sync_with_stdio(false)#define Pi acos(-1)#define pb push_back#define fi first#define se second#define ll long long#define ull unsigned long long #define db double#define Pir pair<int, int>#define PIR pair<Pir, Pir>#define INF 0x3f3f3f3f#define mod (ll)(1e9 + 7)
using namespace std;constint mxn =1e5;struct Node
{int x, t;};int s, e;int mk[mxn];int pr[mxn];voidprime(){fill(pr +1, pr + mxn,1);for(int i =2; i * i < mxn; i ++){if(pr[i])for(int j = i * i; j < mxn; j += i)
pr[j]=0;}}voidbfs(){memset(mk,0,sizeof(mk));
queue<Node> q;
q.push((Node){ s,0});while(! q.empty()){
Node now = q.front(); q.pop();if(now.x == e){printf("%d\n", now.t);return;}//个位for(int i =1; i <10; i +=2){int t = now.x /10*10+ i;if(! mk[t]&& pr[t])
q.push((Node){ t, now.t +1}), mk[t]=1;}//十位for(int i =0; i <10; i ++){int t = now.x %10+ now.x /100*100+ i *10;if(! mk[t]&& pr[t])
q.push((Node){ t, now.t +1}), mk[t]=1;}//百位for(int i =0; i <10; i ++){int t = now.x %100+ now.x /1000*1000+ i *100;/* cout << t <<endl; */if(! mk[t]&& pr[t])
q.push((Node){ t, now.t +1}), mk[t]=1;}//千位for(int i =1; i <10; i ++){int t = now.x %1000+ i *1000;if(! mk[t]&& pr[t])
q.push((Node){ t, now.t +1}), mk[t]=1;}}}intmain(){/* fre(); */int T;scanf("%d",&T);prime();while(T --){scanf("%d %d",&s,&e);bfs();}return0;}
G - Shuffle’m Up POJ - 3087
思路
这题没啥说的,就是在暴力dfs 的时候,我们要 暴力 做标记, 减少不必要的 搜索过程
代码
#include<iostream>#include<algorithm>#include<cstring>#include<queue>#include<map>#include<string>#include<cstdio>voidfre(){freopen("A.txt","r",stdin);freopen("Ans.txt","w",stdout);}voidFre(){freopen("A.txt","r",stdin);}#define ios ios::sync_with_stdio(false)#define Pi acos(-1)#define pb push_back#define fi first#define se second#define ll long long#define ull unsigned long long #define db double#define Pir pair<int, int>#define PIR pair<Pir, Pir>#define INF 0x3f3f3f3f#define mod (ll)(1e9 + 7)
using namespace std;constint mxn =1e5;int n;
string a, b, c;
map<string,int> mp;intdfs(string s,int k){if(s == c)return k;if(! mp[s]){
mp[s]=1;
string t;for(int i =0; i < n; i ++){
t += s[n + i];
t += s[i];}int val =dfs(t, k +1);if(val !=-1)return val;}return-1;}intmain(){
ios;/* fre(); */int T, Case =1;
cin >> T;while(T --){
mp.clear();
cin >> n;
cin >> a >> b >> c;
string t;for(int i =0; i < n; i ++){
t += b[i];
t += a[i];}
cout << Case ++<<" "<<dfs(t,1)<< endl;}return0;}
对于题目给的3中操作,由于是2个物品,所有我们在bfs中可以进行6中操作,我们给这六种操作分别 编上编号为1~6,我们 用这是第几次(设为t次)对两个物品操作设为,那么我们可以用 t * 10 + 该操作编号 = 位置/状态x,这样对于不同的状态就有不同的 标识编号了,这样就能 桶这个 表示 作为并查集的
f
[
]
f[]
f[]的下标了
最后用并查集 递归输出答案所需的格式
代码
#include<iostream>#include<algorithm>#include<cstring>#include<queue>#include<map>#include<string>#include<cstdio>voidfre(){freopen("A.txt","r",stdin);freopen("Ans.txt","w",stdout);}voidFre(){freopen("A.txt","r",stdin);}#define ios ios::sync_with_stdio(false)#define Pi acos(-1)#define pb push_back#define fi first#define se second#define ll long long#define ull unsigned long long #define db double#define Pir pair<int, int>#define PIR pair<Pir, Pir>#define INF 0x3f3f3f3f#define mod (ll)(1e9 + 7)
using namespace std;constint mxn =1e5;int n;
string a, b, c;
map<string,int> mp;intdfs(string s,int k){if(s == c)return k;if(! mp[s]){
mp[s]=1;
string t;for(int i =0; i < n; i ++){
t += s[n + i];
t += s[i];}int val =dfs(t, k +1);if(val !=-1)return val;}return-1;}intmain(){
ios;/* fre(); */int T, Case =1;
cin >> T;while(T --){
mp.clear();
cin >> n;
cin >> a >> b >> c;
string t;for(int i =0; i < n; i ++){
t += b[i];
t += a[i];}
cout << Case ++<<" "<<dfs(t,1)<< endl;}return0;}
#include<iostream>#include<algorithm>#include<cstring>#include<queue>#include<map>#include<string>#include<cstdio>voidfre(){freopen("A.txt","r",stdin);freopen("Ans.txt","w",stdout);}voidFre(){freopen("A.txt","r",stdin);}#define ios ios::sync_with_stdio(false)#define Pi acos(-1)#define pb push_back#define fi first#define se second#define ll long long#define ull unsigned long long #define db double#define Pir pair<int, int>#define PIR pair<Pir, Pir>#define INF 0x3f3f3f3f#define mod (ll)(1e9 + 7)
using namespace std;constint mxn =105;int mk[mxn][mxn][mxn];int x, y, z;struct Node
{int a, b, c, t;};
bool check(Node s){if(s.a !=0&& s.a != x /2)return false;if(s.b !=0&& s.b != x /2)return false;if(s.c !=0&& s.c != x /2)return false;return true;}voidbfs(){memset(mk,0,sizeof(mk));
mk[x][0][0]=1;
queue<Node> q;
q.push((Node){ x,0,0,0});while(! q.empty()){
Node s = q.front(); q.pop();if(check(s)){printf("%d\n", s.t);return;}if(s.a){if(s.b < y){int v =min(s.a, y - s.b);if(! mk[s.a - v][s.b + v][s.c]){
mk[s.a - v][s.b + v][s.c]=1;
q.push((Node){ s.a - v, s.b + v, s.c, s.t +1});}}if(s.c < z){int v =min(s.a, z - s.c);if(! mk[s.a - v][s.b][s.c + v]){
mk[s.a - v][s.b][s.c + v]=1;
q.push((Node){ s.a - v, s.b, s.c + v, s.t +1});}}}if(s.b){if(s.a < x){int v =min(s.b, x - s.a);if(! mk[s.a + v][s.b - v][s.c]){
mk[s.a + v][s.b - v][s.c]=1;
q.push((Node){ s.a + v, s.b - v, s.c, s.t +1});}}if(s.c < z){int v =min(s.b, z - s.c);if(! mk[s.a][s.b - v][s.c + v]){
mk[s.a][s.b - v][s.c + v]=1;
q.push((Node){ s.a, s.b - v, s.c + v, s.t +1});}}}if(s.c){if(s.a < x){int v =min(s.c, x - s.a);if(! mk[s.a + v][s.b][s.c - v]){
mk[s.a + v][s.b][s.c - v]=1;
q.push((Node){ s.a + v, s.b, s.c - v, s.t +1});}}if(s.b < y){int v =min(s.c, y - s.b);if(! mk[s.a][s.b + v][s.c - v]){
mk[s.a][s.b + v][s.c - v]=1;
q.push((Node){ s.a, s.b + v, s.c - v, s.t +1});}}}}printf("NO\n");}intmain(){/* fre(); */while(scanf("%d %d %d",&x,&y,&z)&& x){if(x %2)printf("NO\n");elsebfs();}return0;}
N - Find a way HDU - 2612
思路
对两个人分别 跑一下bfs,之后统计到达 相同地点的时间和,找出最小时间和
代码
#include<iostream>#include<algorithm>#include<cstring>#include<queue>#include<map>#include<string>#include<cstdio>voidfre(){freopen("A.txt","r",stdin);freopen("Ans.txt","w",stdout);}voidFre(){freopen("A.txt","r",stdin);}#define ios ios::sync_with_stdio(false)#define Pi acos(-1)#define pb push_back#define fi first#define se second#define ll long long#define ull unsigned long long #define db double#define Pir pair<int, int>#define PIR pair<Pir, Pir>#define INF 0x3f3f3f3f#define mod (ll)(1e9 + 7)
using namespace std;constint mxn =205;int n, m;char mz[mxn][mxn];int mov[4][2]={{1,0},{0,1},{-1,0},{0,-1}};int mk[mxn][mxn];struct Node
{int x, y, t;}s1, s2;
vector<Node> e;int t1[mxn][mxn], t2[mxn][mxn];
bool check(int x,int y){if(x >=1&& y >=1&& x <= n && y <= m && mz[x][y]!='#'&&! mk[x][y])return true;return false;}voidbfs(Node s,int t[mxn][mxn]){memset(mk,0,sizeof(mk));
mk[s.x][s.y]=1;
queue<Node> q;
q.push(s);while(! q.empty()){
s = q.front(); q.pop();if(mz[s.x][s.y]=='@'&&! t[s.x][s.y])
t[s.x][s.y]= s.t;for(int i =0; i <4; i ++){
Node now =(Node){ s.x + mov[i][0], s.y + mov[i][1], s.t +11};if(!check(now.x, now.y))continue;
q.push(now);
mk[now.x][now.y]=1;}}}intmain(){/* fre(); */while(~scanf("%d %d",&n,&m)){memset(t1,0,sizeof(t1));memset(t2,0,sizeof(t2));
e.clear();for(int i =1; i <= n; i ++){scanf("%s", mz[i]+1);for(int j =1; j <= m; j ++){if(mz[i][j]=='Y')
s1 =(Node){ i, j,0};if(mz[i][j]=='M')
s2 =(Node){ i, j,0};if(mz[i][j]=='@')
e.push_back((Node){ i, j });}}bfs(s1, t1);bfs(s2, t2);int ans = INF;for(auto p : e)if(t1[p.x][p.y]&& t2[p.x][p.y])
ans =min(ans, t1[p.x][p.y]+ t2[p.x][p.y]);printf("%d\n", ans);}return0;}