新人们加油,就优先队列有点难度,其它对新手很有学习价值
下面是我今晚刚刚刷的几道基本搜索题
hdu 1010 Tempter of the Bone
经典搜索入门题,DFS,本题考查要点:剪枝,奇偶性剪枝

#include<stdio.h> #include<stdlib.h> int m,n,t; char map[8][8]; int dir[4][2]={{1,0},{-1,0},{0,-1},{0,1}}; int ex,ey,sx,sy,ok;//e表示end,终点,s表示start,出发点,ok用来判断是否在规定时间到达 void dfs(int x,int y,int cnt) { int i; if(cnt==t)//剪枝1:到时间了符合条件ok=1再退出,不符合条件直接退出。 { if(ex==x&&ey==y)ok=1; return; } if(ok)return;//找到解后还有部分在继续搜索,这条是为了让其它搜索停止 int temp=abs(x-ex)+abs(y-ey)-abs(cnt-t);//剪枝2:((((要点)))) //奇偶性剪枝 ,起点和终点确定以后就可以确定走的步数是奇数还是偶数,通过这个特点来剪枝 if(temp>0||temp&1)return;//temp&1相当于temp%2,运算位。 for(i=0;i<4;i++) { int fx=x+dir[i][0]; int fy=y+dir[i][1]; if(fx>=0&&fx<n&&fy>=0&&fy<m&&map[fx][fy]!='X') { map[fx][fy]='X'; dfs(fx,fy,cnt+1); map[fx][fy]='.'; //回溯 } } } int main() { int i,j,wall; //freopen("int.txt","r",stdin); while(scanf("%d%d%d",&n,&m,&t)!=EOF&&(m||n||t)) { wall=0; for(i=0;i<n;i++) { scanf("%s",map[i]); for(j=0;map[i][j]!='\0';j++) { if(map[i][j]=='S') { sx=i;sy=j; } else if(map[i][j]=='D') { ex=i;ey=j; } else if(map[i][j]=='X') wall++; } } if(m*n-wall<=t) printf("NO\n"); //剪枝3:能走的格子个数比时间少的话,直接不符合,不用再搜了 else { ok=0; map[sx][sy]='X'; dfs(sx,sy,0); if(ok)printf("YES\n"); else printf("NO\n"); } } return 0; }
hdu 1241 Oil Deposits
水题,DFS,适合新手,新人加油

#include<stdio.h> int m,n; char map[101][101]; int dir[8][2]={{1,0},{1,-1},{1,1},{0,-1},{0,1},{-1,0},{-1,-1},{-1,1}};//8个方向 void dfs(int x,int y) { int fx,fy,i; //该题比较水,适合新手,不解释 for(i=0;i<8;i++) { fx=x+dir[i][0]; fy=y+dir[i][1]; if(fx>=0&&fx<n&&fy>=0&&fy<m&&map[fx][fy]=='@') { map[fx][fy]='*'; dfs(fx,fy); } } } int main() { int i,j; //freopen("int.txt","r",stdin); while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0)break; getchar(); for(i=0;i<n;i++) { for(j=0;j<m;j++) { scanf("%c",&map[i][j]); } getchar(); } int cnt=0; for(i=0;i<n;i++) for(j=0;j<m;j++) if(map[i][j]=='@'){map[i][j]='*';cnt++;dfs(i,j);} printf("%d\n",cnt); } return 0; }
hdu 1242 Rescue
优先队列+BFS,注意救援可以有多个,但杭电题目一个救援就可以过

#include<stdio.h> #include<queue> #include<string.h> #define maxn 201 using namespace std; int dir[4][2]={1,0,-1,0,0,-1,0,1}; int m,n,sx,sy; int vis[maxn][maxn]; char map[maxn][maxn]; struct node { int x,y,time; friend bool operator<(node a,node b)//优先队列,去学学STL吧 { return a.time>b.time; } }; int bfs() { node now,next; int i; priority_queue<node>q; while(!q.empty())q.pop(); now.x=sx;now.y=sy;now.time=0; q.push(now);vis[now.x][now.y]=1; while(!q.empty()) { now=q.top(); q.pop(); if(map[now.x][now.y]=='r')return now.time; for(i=0;i<4;i++) { next.x=now.x+dir[i][0]; next.y=now.y+dir[i][1]; if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&map[next.x][next.y]!='#'&&!vis[next.x][next.y]) { if(map[next.x][next.y]=='x')next.time=now.time+2; else next.time=now.time+1; vis[next.x][next.y]=1; q.push(next); } } } return -1; } int main() { int i,j; //freopen("int.txt","r",stdin); while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0)break; getchar(); for(i=0;i<n;i++) { for(j=0;j<m;j++) { scanf("%c",&map[i][j]); if(map[i][j]=='a'){sx=i;sy=j;} } getchar(); } memset(vis,0,sizeof(vis)); int ans=bfs(); if(ans!=-1)printf("%d\n",ans); else printf("Poor ANGEL has to stay in the prison all his life.\n" ); } }
hdu1312 Red and Black
DFS,新手题

#include<stdio.h> int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; int m,n,bx,by,cnt; char map[22][22]; void dfs(int x,int y) { int i;cnt++; for(i=0;i<4;i++) { int fx=x+dir[i][0]; int fy=y+dir[i][1]; if(fx>=0&&fx<n&&fy>=0&&fy<m&&map[fx][fy]=='.') { map[fx][fy]='#' ; dfs(fx,fy); } } } int main() { int i,j; //freopen("int.txt","r",stdin); while(scanf("%d%d",&m,&n)!=EOF) { getchar(); if(m==0&&n==0)break; for(i=0;i<n;i++) { for(j=0;j<m;j++) { scanf("%c",&map[i][j]); if(map[i][j]=='@'){bx=i;by=j;} } getchar(); } cnt=0;map[bx][by]='#'; dfs(bx,by); printf("%d\n",cnt); } return 0; }
hdu 1026 搜索题
BFS+优先队列,因为要输出路径,比较麻烦,所以要用到结构体

#include<stdio.h> #include<queue> #define maxn 101 using namespace std; int m,n; int dir[4][2]={1,0,-1,0,0,-1,0,1}; struct node { int x,y,time; friend int operator<(node a,node b) { return a.time>b.time; } }; struct cmap { char c; int nx,ny; }map[maxn][maxn]; int fight[maxn][maxn],vis[maxn][maxn]; priority_queue<node>q; int bfs() { node now,next; int i; char s; while(!q.empty())q.pop(); now.x=n-1;now.y=m-1; s=map[now.x][now.y].c; if(s>='1'&&s<='9') { now.time=map[now.x][now.y].c-'0'; fight[now.x][now.y]=map[now.x][now.y].c-'0'; } else now.time=0; q.push(now); while(!q.empty()) { now=q.top();q.pop(); if(now.x==0&&now.y==0)return now.time; for(i=0;i<4;i++) { next.x=now.x+dir[i][0]; next.y=now.y+dir[i][1]; if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&map[next.x][next.y].c!='X'&&!vis[next.x][next.y]) { vis[next.x][next.y]=1; s=map[next.x][next.y].c; if(s>='1'&&s<='9') { fight[next.x][next.y]=s-'0'; next.time=now.time+s-'0'+1; } else next.time=now.time+1; map[next.x][next.y].nx=now.x; map[next.x][next.y].ny=now.y; q.push(next); } } } return -1; } int main() { int i,j; //freopen("int.txt","r",stdin); while(scanf("%d%d",&n,&m)!=EOF) { getchar(); for(i=0;i<n;i++) { for(j=0;j<m;j++) { scanf("%c",&map[i][j].c); } getchar(); } memset(vis,0,sizeof(vis)); memset(fight,0,sizeof(fight)); int ans=bfs(); if(ans==-1)printf("God please help our poor hero.\n"); else { printf("It takes %d seconds to reach the target position, let me show you the way.\n",ans); int cnt=1,x=0,y=0; while(cnt<=ans) { int fx=map[x][y].nx,fy=map[x][y].ny; printf("%ds:(%d,%d)->(%d,%d)\n",cnt++,x,y,fx,fy); for(int j=1;j<=fight[fx][fy];j++) printf("%ds:FIGHT AT (%d,%d)\n",cnt++,fx,fy); x=fx;y=fy; } } printf("FINISH\n"); } return 0; }