专题训练一简单搜索

专题训练一简单搜索
A - 棋盘问题 POJ - 1321
简单的深度优先搜索,类似于八皇后问题

#include<iostream>
#include<algorithm>
#include<string>
#include<fstream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
using namespace std;
const int inf=0x7fffffff;
const int MAXN=1;
char mp[20][20];
bool rol[20];
int n,k;
int ans=0;
void dfs(int c,int x) {
	if(x>=k) {
		ans++;
		return ;
	}
	for(int i=c; i<n; i++) {
		for(int j=0; j<n; j++) {
			if(!rol[j]&&mp[i][j]=='#') {
				rol[j]=true;
				dfs(i+1,x+1);
				rol[j]=false;
			}
		}
	}
	return ;
}
int main() {
	while(cin>>n>>k) {
		if((n==-1&&k==-1)){
			break;
		}
		memset(rol,false,sizeof(rol));
		for(int i=0; i<n; i++) {
			cin>>mp[i];
		}
		ans=0;
		dfs(0,0);
		cout<<ans<<endl;
	}
	return 0;
}

B - Dungeon Master POJ - 2251
三维空间搜索,用bfs,dfs超时。

#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<vector>
#include<map>
#include<queue>
#include<bitset>
#include<list>
#include<stack>
#include<iterator>
#include<fstream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
using namespace std;
const int inf=0x7fffffff;
const int MAXN=32;
char mp[MAXN][MAXN][MAXN];
int r,c,l;
class Point {
	public:
		int x,y,z;
		Point(int d,int e,int f) {
			x=d;
			y=e;
			z=f;
		}
		Point() {
		}
};
Point read(int &x,int &y,int &z) {
	for(int i=0; i<l; i++) {
		for(int j=0; j<r; j++) {
			for(int k=0; k<c; k++) {
				cin>>mp[i][j][k];
			}
		}
		getchar();
	}
	Point q;
	for(int i=0; i<l; i++) {
		for(int j=0; j<r; j++) {
			for(int k=0; k<c; k++) {
				if(mp[i][j][k]=='S') {
					x=i;
					y=j;
					z=k;
				}
				if(mp[i][j][k]=='E') {
					q.x=i;
					q.y=j;
					q.z=k;
				}
			}
		}
	}
	return q;
}
int res[MAXN][MAXN][MAXN];
const int step[][3]= {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
void  bfs(int x,int y,int z) {
	memset(res,0,sizeof(res));
	queue<Point>q;
	Point s(x,y,z);
	q.push(s);
	while(!q.empty()) {
		Point tmp=q.front();
		q.pop();
		for(int i=0; i<6; i++) {
			Point temp=tmp;
			temp.x+=step[i][0];
			temp.y+=step[i][1];
			temp.z+=step[i][2];
			if(temp.x<0||temp.y<0||temp.z<0||temp.x>=l||temp.y>=r||temp.z>=c||mp[temp.x][temp.y][temp.z]=='#') {
				continue;
			}
			if(res[temp.x][temp.y][temp.z]==0) {
				res[temp.x][temp.y][temp.z]=res[tmp.x][tmp.y][tmp.z]+1;
				q.push(temp);
			}
		}
	}
}
int main() {
	int z,x,y;
	while(cin>>l>>r>>c&&l&&c&&r) {
		Point ans=read(x,y,z);
		//cout<<ans.x<<" "<<ans.y<<" "<<ans.z<<endl;
		bfs(x,y,z);
		if(res[ans.x][ans.y][ans.z]==0) {
			cout<<"Trapped!"<<endl;
		} else {
			cout<<"Escaped in "<<res[ans.x][ans.y][ans.z]<<" minute(s)."<<endl;
		}
	}
	return 0;
}

C - Catch That Cow POJ - 3278
题目大意即在一个数轴上,有三个选择,可以左移一步,右移一步,或者移动到当前位置乘二的位置,问需要几步移动到目的位置。bfs即可,以起始点作为初始起点,转移步骤为加一,减一和乘二。

#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<vector>
#include<map>
#include<queue>
#include<bitset>
#include<list>
#include<stack>
#include<iterator>
#include<fstream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
using namespace std;
const int inf=0x7ffffffe;
const int MAXN=100005;
int n,k;
int visit[MAXN];
int step[MAXN];

int bfs() {
	queue<int>q;
	memset(visit,0,sizeof(visit));
	memset(step,0,sizeof(step));
	q.push(n);
	visit[n]=1;
	step[n]=0;
	while(!q.empty()) {
		int x=q.front();
		q.pop();
		int t;
		for(int i=0; i<3; i++) {
			if(i==0) {
				t=x+1;
			} else if(i==1) {
				t=x-1;
			} else {
				t=x*2;
			}
			if(t>=MAXN||t<0) {
				continue;
			}
			if(visit[t]==0) {
				q.push(t);
				visit[t]=1;
				step[t]=step[x]+1;
			}
		}
	}
}
int main() {
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	while(cin>>n>>k) {
		if(n>=k) {
			cout<<n-k<<endl;
		} else {
			bfs();
			cout<<step[k]<<endl;
		}
	}
	return 0;
}

D - Fliptile POJ - 3279
暴力枚举中的经典问题,因为后边的灯的熄灭方法只取决与前一排,枚举第一排灯的状态即可,看最后一排灯能否全部熄灭。

#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<vector>
#include<map>
#include<queue>
#include<bitset>
#include<list>
#include<stack>
#include<iterator>
#include<fstream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
using namespace std;
const int inf=0x7fffffff;
const int MAXN=1;
int ori[20];
int li[20];
int res[20];
int get_bit(int c,int i) {
	return (c>>i)&1;
}
void set_bit(int & c,int i,int v) {
	if(v) {
		c|=(1<<i);
	} else {
		c&=~(1<<i);
	}
}
void flip_bit(int &c,int i) {
	c^=(1<<i);
}
int main() {
	int n,m;
	while(~scanf("%d%d",&n,&m)) {
		for(int i=0; i<n; i++) {
			for(int j=0; j<m; j++) {
				int s;
				scanf("%d",&s);
				set_bit(ori[i],j,s);
			}
		}
		int F=1;
		for(int x=0; x<1<<m; x++) {
			int swc=x;
			memcpy(li,ori,sizeof(ori));
			for(int i=0; i<n; i++) {
				res[i]=swc;
				for(int j=0; j<m; j++) {
					if(get_bit(swc,j)) {
						if(j>0) {
							flip_bit(li[i],j-1);
						}
						flip_bit(li[i],j);
						if(j<m-1) {
							flip_bit(li[i],j+1);
						}
					}
				}
				if(i<n-1) {
					li[i+1]^=swc;
				}
				swc=li[i];
			}
			if(li[n-1]==0) {
				for(int i=0; i<n; i++) {
					for(int j=0; j<m; j++) {
						if(j!=0) {
							printf(" ");
						}
						printf("%d",get_bit(res[i],j));
					}
					printf("\n");
				}
				F=0;
				break; 
			}
		}
		if(F){
			cout<<"IMPOSSIBLE"<<endl;
		}
	}
	return 0;
}

E - Find The Multiple POJ - 1426
简单的bfs,转移到下一步有两种方法,加一,或者乘十。

#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<vector>
#include<map>
#include<queue>
#include<bitset>
#include<list>
#include<stack>
#include<iterator>
#include<fstream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
using namespace std;
const int inf=0x7fffffff;
const int MAXN=1;
void bfs(long long x){
	queue<long long>q;
	q.push(1);
	while(!q.empty()){
		long long a=q.front();
		q.pop();
		if(a%x==0){
			cout<<a<<endl;
			return ;
		}
		q.push(a*10);
		q.push(a*10+1);
	}
}
int main(){
	int n;
	while(cin>>n&&n){
		bfs(n);
	}
	return 0;
}

F - Prime Path POJ - 3126
求最少需要多少次修改,从给的数字出发,bfs所有能修改到的数字,输出最后的步数即可(预处理用欧拉筛法筛选区间中的素数)。

#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<vector>
#include<map>
#include<queue>
#include<bitset>
#include<list>
#include<stack>
#include<iterator>
#include<fstream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
using namespace std;
const int inf=0x7fffffff;
const int MAXN=1;
bool s[10009];
int su[10009];
int num=0;
void prime() {
	memset(s,true,sizeof(s));
	s[0]=s[1]=false;
	for(int i=2; i<10005; i++) {
		if(s[i]) {
			su[num++]=i;
		}
		for(int j=0; j<num; j++) {
			if(su[j]*i>10005) {
				break;
			}
			s[i*su[j]]=false;
			if(i%su[j]==0) {
				break;
			}
		}
	}
}
int step[10005];
bool visit[10005];
int change(int number,int i,int j) {
	if(i==0) {
		return j*1000+number%1000;
	} else if(i==1) {
		return (number/1000)*1000+j*100+number%100;
	} else if(i==2) {
		return (number/100)*100+j*10+number%10;
	} else {
		return (number/10)*10+j;
	}
}
int bfs(int a,int b) {
	memset(step,0,sizeof(step));
	memset(visit,false,sizeof(visit));
	queue<int>q;
	q.push(a);
	visit[a]=true;
	step[a]=0;
	while(!q.empty()) {
		int head=q.front();
		q.pop();
		int next;
		for(int i=0; i<=3; i++) {
			if(i==0) {
				for(int j=1; j<10; j++) {
					next=change(head,i,j);
					if(s[next]&&!visit[next]) {
						q.push(next);
						visit[next]=true;
						step[next]=step[head]+1;
						//cout<<"next:  "<<next<<" "<<step[next]<<endl;
					}
				}
			} else {
				for(int j=0; j<10; j++) {
					int next=change(head,i,j);
					if(s[next]&&!visit[next]) {
						
						q.push(next);
						visit[next]=true;
						step[next]=step[head]+1;
						//cout<<"next:  "<<next<<" "<<step[next]<<endl;
					}
				}
			}
			
			if(visit[b]){
				return step[b];
			}
		}
	}
	return -1;
}
int main() {
	prime();
	//freopen("a.txt","w",stdout);
	int N;
	cin>>N;
	while(N--){
		int a,b;
		cin>>a>>b;
		int ans=bfs(a,b);
		if(ans==-1){
			cout<<"Impossible"<<endl;
		}else{
			cout<<ans<<endl;
		}
	}
	return 0;
}

G - Shuffle’m Up POJ - 3087
暴力模拟

#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<vector>
#include<map>
#include<queue>
#include<bitset>
#include<list>
#include<stack>
#include<iterator>
#include<fstream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
using namespace std;
const int inf=0x7fffffff;
const int MAXN=1;
char s1[405];
char s2[405];
map<string ,bool>mp;

int main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	int N;
	cin>>N;
	int A=1;
	while(N--){
		int n;
		cin>>n;
		mp.clear();
		getchar();
		gets(s1);
		gets(s2);
		string ans;
		getline(cin,ans);
		int cnt=0;
		while(1){
			string t;
			for(int i=0;i<n;i++){
				t=t+s2[i];
				t=t+s1[i];
			}
			cnt++;
			if(t==ans){
				cout<<A++<<" "<<cnt<<endl;
				break;
			}
			if(mp[t]){
				cout<<A++<<" -1"<<endl;
				break;
			}
			mp[t]=true;
			for(int i=0;i<n;i++){
				s1[i]=t[i];
			}
			for(int i=0;i<n;i++){
				s2[i]=t[n+i];
			}
		}
	}
	return 0;
}

H - Pots POJ - 3414
搜索比较复杂,先用bfs搜索所有的状态,同时记录路径,输出结果时,将当前节点到根的路径打印即可。

#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<vector>
#include<map>
#include<queue>
#include<bitset>
#include<list>
#include<stack>
#include<iterator>
#include<fstream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
using namespace std;
int m1,m2;
const int inf=0x7fffffff;
const string st[10]={"","FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
const int MAXN=1;

class Pot {
	public:
		int fa1,fa2;
		int now1,now2;
		int step;
};
Pot pot[105][105];
int cnt=0;
int flag[105][105];
queue<Pot>p;
Pot fill(Pot a,int sel) {
	Pot t;
	t.fa1=a.now1;
	t.fa2=a.now2;
	if(sel==1) {
		t.now1=m1;
		t.now2=a.now2;
		t.step=1;
	} else {
		t.now2=m2;
		t.now1=a.now1;
		t.step=2;
	}
	return t;
}
Pot drop(Pot a,int sel) {
	Pot t;
	t.fa1=a.now1;
	t.fa2=a.now2;
	if(sel==1) {
		t.now1=0;
		t.now2=a.now2;
		t.step=3;
	} else {
		t.now2=0;
		t.now1=a.now1;
		t.step=4;
	}
	return t;
}
Pot pour(Pot a,int sel){
	Pot t;
	t.fa1=a.now1;
	t.fa2=a.now2;
	if(sel==1) {
		if(m2-a.now2>=a.now1){
			t.now1=0;
			t.now2=a.now2+a.now1;
		}else{
			t.now1=a.now1-(m2-a.now2);
			t.now2=m2;
		}
		t.step=5;
	} else {
		if(m1-a.now1>=a.now2){
			t.now2=0;
			t.now1=a.now1+a.now2;
		}else{
			t.now2=a.now2-(m1-a.now1);
			t.now1=m1;
		}
		t.step=6;
	}
	return t;
}
stack<string>s;
int main() {
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	int a,b,c;
	while(~scanf("%d%d%d",&m1,&m2,&c)) {
		memset(pot,0,sizeof(pot));
		memset(flag,0,sizeof(flag));
		while(!p.empty()) {
			p.pop();
		}
		Pot x;
		x.fa1=-1;
		x.fa2=-1;
		x.now1=0;
		x.now2=0;
		x.step=0;
		p.push(x);
		flag[0][0]=1;
		Pot res;
		res.step=0;
		
		while(!p.empty()) {
			Pot head=p.front();
			if(head.now1==c||head.now2==c){
				res=head;
				break;
			}
			//cout<<"head "<<head.now1<<"  "<<head.now2<<endl;
			p.pop();
			Pot t=fill(head,1);
			//cout<<"t1  "<<t.now1<<" "<<t.now2<<endl;
			if(flag[t.now1][t.now2]==0){
				p.push(t);
				flag[t.now1][t.now2]=1;
				pot[t.now1][t.now2]=t;
			}
			t=fill(head,2);
			//cout<<"t2  "<<t.now1<<" "<<t.now2<<endl;
			if(flag[t.now1][t.now2]==0){
				p.push(t);
				flag[t.now1][t.now2]=1;
				pot[t.now1][t.now2]=t;
			}
			t=drop(head,1);
			//cout<<"t3  "<<t.now1<<" "<<t.now2<<endl;
			if(flag[t.now1][t.now2]==0){
				p.push(t);
				flag[t.now1][t.now2]=1;
				pot[t.now1][t.now2]=t;
			}
			t=drop(head,2);
			//cout<<"t4  "<<t.now1<<" "<<t.now2<<endl;
			if(flag[t.now1][t.now2]==0){
				p.push(t);
				flag[t.now1][t.now2]=1;
				pot[t.now1][t.now2]=t;
			}
			t=pour(head,1);
			//cout<<"t5  "<<t.now1<<" "<<t.now2<<endl;
			if(flag[t.now1][t.now2]==0){
				p.push(t);
				flag[t.now1][t.now2]=1;
				pot[t.now1][t.now2]=t;
			}
			t=pour(head,2);
			if(flag[t.now1][t.now2]==0){
				p.push(t);
				flag[t.now1][t.now2]=1;
				pot[t.now1][t.now2]=t;
			}
		}
		int cnt=0;
		if(res.step==0) {
			cout<<"impossible"<<endl;
		} else {
			while(!s.empty()) {
				s.pop();
			}
			while(res.step!=0) {
				s.push(st[res.step]);
				cnt++;
				res=pot[res.fa1][res.fa2];
			}
			cout<<cnt<<endl;
			while(!s.empty()) {
				cout<<s.top()<<endl;
				s.pop();
			}
		}
	}
	return 0;
}

I - Fire Game FZU - 2150
题意:从一点出发,最快走完所有的“#”。
用bfs搜索图中的“#”,并记录走过的“#”的个数,在多个“#”所bfs到的结果中找最小值。

#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<vector>
#include<map>
#include<queue>
#include<bitset>
#include<list>
#include<stack>
#include<iterator>
#include<fstream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
using namespace std;
const int inf=0x3f3f3f3f;
const int MAXN=1;
char mp[15][15];
int n,m;
int read() {
	cin>>n>>m;
	getchar();
	for(int i=0; i<n; i++) {
		gets(mp[i]);
	}
	int sum=0;
	for(int i=0; i<n; i++) {
		for(int j=0; j<m; j++) {
			if(mp[i][j]=='#') {
				sum++;
			}
		}
	}
	return sum;
}
class A {
	public:
		int  a,b;
		int step;
};
class B {
	public:
		int sum;
		int tim;
};
const int s[][2]= {{0,1},{1,0},{0,-1},{-1,0}};
queue<A>q;
int flag[20][20];
int step[20][20];
B bfs(int x,int y,int a,int b) {
	while(!q.empty()) {
		q.pop();
	}
	memset(flag,0,sizeof(flag));
	memset(step,inf,sizeof(step));
	A t;
	if(mp[x][y]=='#') {
		t.a=x;
		t.b=y;
		t.step=0;
		q.push(t);
		flag[x][y]=1;

	}
	if(mp[a][b]=='#') {
		t.a=a;
		t.b=b;
		t.step=0;
		q.push(t);
		flag[a][b]=1;
	}
	int sum=0;
	int maxn=0	;
	while(!q.empty()) {
		A head=q.front();
		q.pop();
		step[head.a][head.b]=min(step[head.a][head.b],head.step);
		for(int i=0; i<4; i++) {
			t.a=head.a+s[i][0];
			t.b=head.b+s[i][1];
			if(t.a>=0&&t.a<n&&t.b>=0&&t.b<m&&mp[t.a][t.b]=='#'&&flag[t.a][t.b]==0) {
				t.step=head.step+1;
				q.push(t);
				flag[t.a][t.b]=1;
			}
		}
	}
	for(int i=0; i<n; i++) {
		for(int j=0; j<m; j++) {
			if(mp[i][j]=='#') {
				sum+=flag[i][j];
				maxn=max(maxn,step[i][j]);
			}
		}
	}
	B res;
	res.sum=sum;
	res.tim=maxn;
	return res;
}
int main() {
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	int T;
	while(cin>>T) {
		for(int K=1; K<=T; K++) {
			int cnt=read();
			//	cout<<"cnt  "<<cnt<<endl;
			int step=0x7fffffff;
			for(int i1=0; i1<n; i1++) {
				for(int j1=0; j1<m; j1++) {
					for(int i2=0; i2<n; i2++) {
						for(int j2=0; j2<m; j2++) {
							B res=bfs(i1,j1,i2,j2);
							step=min(step,res.tim);
						}
					}
				}
			}
			if(step==0x3f3f3f3f) {
				step=-1;
			}
			cout<<"Case "<<K<<": "<<step<<endl;
		}
	}
	return 0;
}

J - Fire!
两次bfs,第一次标出火走的没一步,第二次bfs寻找能否走出去的路。

#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<vector>
#include<map>
#include<queue>
#include<bitset>
#include<list>
#include<stack>
#include<iterator>
#include<fstream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
using namespace std;
const int inf=0x7fffffff;
const int MAXN=1;
int n,m;
char mp[1005][1005];
void read(){
	for(int i=0;i<n;i++){
		gets(mp[i]);
	}
} 
int fx,fy;
int jx,jy;
int step[1005][1005];
bool visit[1005][1005];
int res[1005][1005];
class A{
public:
	int x,y;
	int step;
};
queue<A>q;
queue<A>f;
const int s[][2]= {{0,1},{1,0},{0,-1},{-1,0}};
void bfs1(){
	memset(visit,false,sizeof(visit));
	memset(step,0x3f3f3f3f,sizeof(step));
	A t;
	while(!q.empty()){
		A head =q.front();
		q.pop();
		step[head.x][head.y]=head.step;
		for(int i=0;i<4;i++){
			t.x=head.x+s[i][0];
			t.y=head.y+s[i][1];
			t.step=head.step+1;
			//cout<<t.x<<" "<<t.y<<endl;
			if(t.x>=0&&t.x<n&&t.y>=0&&t.y<m&&mp[t.x][t.y]=='.'&&visit[t.x][t.y]==false){
				visit[t.x][t.y]=true;
				q.push(t);
			}
		}
	}
}
int bfs(){
	A t;
	t.x=jx;
	t.y=jy;
	t.step=0;
	q.push(t);
	memset(visit,false,sizeof(visit));
	memset(res,0x3f3f3f3f,sizeof(res));
	visit[jx][jy]=true;
	res[jx][jy]=t.step;
	while(!q.empty()){
		A head =q.front();
		if(head.step<step[head.x][head.y]){
			res[head.x][head.y]=head.step;
		}
		q.pop();
		for(int i=0;i<4;i++){
			t.x=head.x+s[i][0];
			t.y=head.y+s[i][1];
			t.step=head.step+1;
			if(t.x>=0&&t.x<n&&t.y>=0&&t.y<m&&mp[t.x][t.y]=='.'&&visit[t.x][t.y]==false){
				visit[t.x][t.y]=true;
				q.push(t);
			}
		}
	}
	int maxn=0x3f3f3f3f;
	//cout<<"asda  ";
	for(int i=0;i<n;i++){
		maxn=min(maxn,res[i][0]);
		//cout<<res[i][0]<<" ";
	}
	//cout<<endl;
	for(int i=0;i<n;i++){
		maxn=min(maxn,res[i][m-1]);
	}
	for(int i=0;i<m;i++){
		maxn=min(maxn,res[0][i]);
	}
	for(int i=0;i<m;i++){
		maxn=min(maxn,res[n-1][i]);
	}
	return maxn;
}
int main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	int T;
	cin>>T;
	while(T--){
		cin>>n>>m;
		getchar();
		read();
		while(!q.empty()){
			q.pop();
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(mp[i][j]=='F'){
					A t;
					t.x=i;
					t.y=j;
					t.step=0;
					q.push(t);
					visit[i][j]=true;
					mp[i][j]='.';
				}
				if(mp[i][j]=='J'){
					jx=i;
					jy=j;
					mp[i][j]='.';
				}
			}
		}
		bfs1();
		if(res1==0x3f3f3f3f){
			cout<<"IMPOSSIBLE"<<endl;
		}else{
			cout<<res1+1<<endl;
		}
	}
	return 0;
}

K - 迷宫问题 POJ - 3984
bfs即可

#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<vector>
#include<map>
#include<queue>
#include<bitset>
#include<list>
#include<stack>
#include<iterator>
#include<fstream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
using namespace std;
const int inf=0x7fffffff;
const int MAXN=1;
class Point {
	public:
		int a,b;
		Point(int x,int y) {
			a=x;
			b=y;
		}
		Point(){
		
		}
};
int mp[6][6];
Point qn[8][8];
bool judge(int x,int y) {
	if(x<0||y>4||x>4||y<0||mp[x][y]==1) {
		return true;
	}
	return false;
}
const int f[][2]= {{0,-1},{0,1},{1,0},{-1,0}};
void bfs() {
	queue<Point>q;
	q.push(Point(0,0));
	mp[0][0]=1;
	while(!q.empty()) {
		Point a=q.front();
		q.pop();
		for(int i=0; i<4; i++) {
			Point b=a;
			b.a+=f[i][0];
			b.b+=f[i][1];
			if(judge(b.a,b.b)) {
				continue;
			}
			qn[b.a][b.b]=Point(b.a-f[i][0],b.b-f[i][1]);
			mp[b.a][b.b]=1;
			q.push(b);
			if(b.a==4&&b.b==4){
				return ;
			}
		}
	}
	cout<<"asdasd"<<endl;
}
int main() {
	for(int i=0; i<5; i++) {
		for(int j=0; j<5; j++) {
			cin>>mp[i][j];
		}
	}
	bfs();
	stack<Point>s;
	Point a=qn[4][4];
	while(a.a!=0||a.b!=0){
		s.push(a);
		a=qn[a.a][a.b];
	}
	s.push(Point(0,0));
	while(!s.empty()){
		cout<<"("<<s.top().a<<", "<<s.top().b<<")"<<endl;
		s.pop();
	}
	cout<<"(4, 4)"<<endl;
	return 0;
}

L - Oil Deposits HDU - 1241
经典的bfs,模板题

#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<vector>
#include<map>
#include<queue>
#include<bitset>
#include<list>
#include<stack>
#include<iterator>
#include<fstream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
using namespace std;
const int inf=0x7fffffff;
const int MAXN=105;
char mp[MAXN][MAXN];
int vis[MAXN][MAXN];
int n,m;
class A{
public:
	int x;int y;
};
const int S[8][2]={{1,0},{-1,0},{0,-1},{0,1},{1,1},{1,-1},{-1,1},{-1,-1}};
int num;
void bfs(int x,int y){
	queue<A>q;
	A a;
	a.x=x;
	a.y=y;
	vis[x][y]=num;
	q.push(a);
	while(!q.empty()){
		A head=q.front();
		q.pop();
		for(int i=0;i<8;i++){
			A t=head;
			t.x+=S[i][0];
			t.y+=S[i][1];
			if(t.x>=0&&t.x<n&&t.y>=0&&t.y<m&&vis[t.x][t.y]==0&&mp[t.x][t.y]=='@'){
				vis[t.x][t.y]=num;
				q.push(t);
			}
		}
	}
}
int main(){
	while(~scanf("%d%d",&n,&m)&&n&&m){
		getchar();
		memset(vis,0,sizeof(vis));
		for(int i=0;i<n;i++){
			gets(mp[i]);
		}
//		for(int i=0;i<n;i++){
//			for(int j=0;j<m;j++){
//				cout<<mp[i][j]<<" ";
//			}
//			cout<<endl;
//		}
		num=0;
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(vis[i][j]==0&&mp[i][j]=='@'){
					num++;
					bfs(i,j);
				}
			}
		}
//		for(int i=0;i<n;i++){
//			for(int j=0;j<m;j++){
//				cout<<vis[i][j]<<" ";
//			}
//			cout<<endl;
//		}
		printf("%d\n",num);
	}
	return 0;
}

M - 非常可乐 HDU - 1495
类似于H题。

#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<vector>
#include<map>
#include<queue>
#include<bitset>
#include<list>
#include<stack>
#include<iterator>
#include<fstream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
using namespace std;
const int inf=0x7fffffff;
const int MAXN=1;
class A{
public:
	int has[5];
	int step;
};
int maxn[5];
A pore(A now,int i,int j){
	A t=now;
	if(now.has[i]>maxn[j]-now.has[j]){
		t.has[i]-=maxn[j]-now.has[j];
		t.has[j]=maxn[j];
	}else{
		t.has[j]+=now.has[i];
		t.has[i]=0;
	}
	t.step++;
	return t;
}
const int s[][2]={{0,1},{0,2},{1,2},{1,0},{2,0},{2,1}};
queue<A>q;
int flag[105][105];
int bfs(){
	memset(flag,0,sizeof(flag));
	while(!q.empty()){
		q.pop();
	}
	A t;
	t.has[0]=maxn[0];
	t.has[1]=0;
	t.has[2]=0;
	t.step=0;
	q.push(t);
	flag[0][0]=1;
	while(!q.empty()){
		A head=q.front();
		//cout<<head.has[0]<<"  "<<head.has[1]<<"  "<<head.has[2]<<"  "<<head.step<<endl;
		//cout<<head.has[0]<<" "<<head.has[1]<<" "<<head.has[2]<<endl;
		if((head.has[0]==maxn[0]/2.0&&head.has[2]==0)||(head.has[0]==maxn[0]/2.0&&head.has[1]==0)||(head.has[1]==maxn[0]/2.0&&head.has[0]==0)){
			return head.step;
		}
		q.pop();
		for(int i=0;i<6;i++){
			t=pore(head,s[i][0],s[i][1]);
			if(flag[t.has[1]][t.has[2]]==0){
				q.push(t);	
				flag[t.has[1]][t.has[2]]=1;
			}
		}
	}
	return inf;
}
int main(){
	while(cin>>maxn[0]>>maxn[1]>>maxn[2]&&(maxn[0]||maxn[1]||maxn[2])){
		int ans=bfs();
		if(ans==inf){
			cout<<"NO"<<endl;
		}else{
			cout<<ans<<endl;
		}
	}
	
	return 0;
}

N - Find a way HDU - 2612
模板题

#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<vector>
#include<map>
#include<queue>
#include<bitset>
#include<list>
#include<stack>
#include<iterator>
#include<fstream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
using namespace std;
const int inf=0x7fffffff;
const int MAXN=205;
int n,m;
int yx,yy,mx,my;
char mp[MAXN][MAXN];
void read() {
	for(int i=0; i<n; i++) {
		gets(mp[i]);
	}
}

void fin() {
	for(int i=0; i<n; i++) {
		for(int j=0; j<m; j++) {
			if(mp[i][j]=='Y') {
				yx=i;
				yy=j;
			}
			if(mp[i][j]=='M') {
				mx=i;
				my=j;
			}
		}
	}
}
class A {
	public:
		int x,y;
};
int step1[MAXN][MAXN];
int step2[MAXN][MAXN];
bool vis[MAXN][MAXN];
const int S[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
void dou_bfs() {
	queue<A>q1;
	queue<A>q2;
	A a;
	memset(vis,false,sizeof(vis));
	a.x=yx;
	a.y=yy;
	q1.push(a);
	vis[a.x][a.y]=true;
	while(!q1.empty()) {
		A head=q1.front();
		q1.pop();
		for(int i=0; i<4; i++) {
			A t=head;
			t.x+=S[i][0];
			t.y+=S[i][1];
			if(t.x>=0&&t.x<n&&t.y>=0&&t.y<m&&mp[t.x][t.y]!='#'&&vis[t.x][t.y]==false) {
				step1[t.x][t.y]=step1[head.x][head.y]+1;
				vis[t.x][t.y]=true;
				q1.push(t);
			}
		}
	}
	a.x=mx;
	a.y=my;
	q2.push(a);
	memset(vis,false,sizeof(vis));
	vis[a.x][a.y]=true;
	while(!q2.empty()) {
		A head=q2.front();
		q2.pop();
		for(int i=0; i<4; i++) {
			A t=head;
			t.x+=S[i][0];
			t.y+=S[i][1];
			if(t.x>=0&&t.x<n&&t.y>=0&&t.y<m&&mp[t.x][t.y]!='#'&&vis[t.x][t.y]==false) {
				step2[t.x][t.y]=step2[head.x][head.y]+1;
				vis[t.x][t.y]=true;
				q2.push(t);
			}
		}
	}
}
int main() {
	while(~scanf("%d%d",&n,&m)) {
		getchar();
		read();
		fin();
		memset(step1,0,sizeof(step1));
		memset(step2,0,sizeof(step2));
		//step1[yx][yy]=step2[mx][my]=1;
		dou_bfs();
		int ans=0x3f3f3f3f;
		//cout<<ans<<endl;
		for(int i=0; i<n; i++) {
			for(int j=0; j<m; j++) {
				if(mp[i][j]=='@'&&step1[i][j]!=0&&step2[i][j]!=0) {
					ans=min(ans,step1[i][j]+step2[i][j]);
				}
			}
		}
		ans*=11;
		printf("%d\n",ans);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流浪天涯无迹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值