《挑战程序设计竞赛》2.1 最基础的穷竭搜索

来源:互联网 发布:淘宝网充值 编辑:程序博客网 时间:2024/05/21 04:20

poj 2386  Lake Counting(裸dfs)

题意:n*m的矩阵 W是水 .是地 问有多少池塘。(池塘的定义是: W通过八个方向连接成的一片算作是一个池塘。)

#include <iostream>using namespace std;#include <cstdio>const int maxn = 100 + 5;int n, m;char ma[maxn][maxn];int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};#define judge(x, y) 0 <= x && x < n && 0 <= y && y < m && ma[x][y] == 'W'void dfs(int x, int y){    ma[x][y] = '.';//这个处理是为了避免对该点重复操作。    for(int i = 0; i < 8; i++)    {        int fx = x + dx[i];        int fy = y + dy[i];        if(judge(fx, fy))            dfs(fx, fy);    }}int main(){    while(~scanf("%d%d", &n, &m))    {        for(int i = 0; i < n; i++)            scanf("%s", ma[i]);        int ans = 0;        for(int i = 0;i < n; i++)            for(int j = 0; j < m; j++)                if(judge(i, j))                    dfs(i, j), ans++;        printf("%d\n", ans);    }    return 0;}


 

poj 1979  Red and Black(裸dfs)

题意:题意:给你一个row*col的矩阵,上面的'#'代表你不能走的地方,'.'表示你能走的地方,'@'表示你的起点,问你最多能走多少格。

#include <iostream>#include <cstdio>using namespace std;const int maxn = 20 + 5;int n, m, sx, sy, cnt;int dx[] = {0, 0, 1, -1};int dy[] = {1, -1, 0, 0};char ma[maxn][maxn];#define judge(x, y) 0 <= x && x < n && 0 <= y && y < mvoid dfs(int x, int y){    cnt++;    ma[x][y] = '#';    for(int i = 0; i < 4; i++)    {        int fx = x + dx[i],fy = y + dy[i];        if(judge(fx, fy) && ma[fx][fy] == '.' )        {            dfs(fx ,fy);        }    }}int main(){    while(~scanf("%d%d", &m, &n) && m && n)//注意终止条件    {        for(int i = 0; i<n; i++)        {            for(int j = 0; j<m; j++)            {                cin >> ma[i][j];                if(ma[i][j] == '@')                    sx = i,sy = j;            }        }        cnt = 0;        dfs(sx, sy);        printf("%d\n", cnt);    }    return 0;}


 

poj 3009  Curling 2.0(dfs)

题意:给定一个m*n的网格,在这些网格上一些地方有障碍物,给定起点与终点的位置,当石头从起点开始走,撞上障碍才会转弯,否则会一直沿着来时的方向继续前进。撞到障碍后停在障碍前的位置,障碍消失。然后石头可以选择四个方向(相邻处无障碍的方向)前进,问至少需要停多少次才能从起点到达终点。不能到达或者多余10步后游戏失败。如果能到达输出最少的步数,否则输出-1.

思路:

  在写dfs的时候,经常先写剪枝,再写终止条件(毕竟是递归,递归的终止是很重要的),再写一系列的搜索操作。首先我们发现这道题不一样的地方,在于每次移动的距离是不定的,所以我们采用一个while循环来进行不断的移动。这道题目,很关键一个地方是学会思考,怎么去思考,要从上往下思考,这道题的思路就非常清晰,而不会写的头晕脑转。

  怎么去思考,你每移动到一个新格子,要整体的来看,一共有四种可能吧,0,1,2,3。那么就是四个分支。

  if(==1) else if(==2) else if(==3) else   

  我们可以发现0和2是可以合并的,那么相当于这个if语句只有三块。我们 定好了大方向,再去完善每个分支。

    1) 0或2:可以移动

    2)1:这个时候碰到的墙可以撞开,那么就更改ma[fx][fy]=0,其次球是要停在墙之前的,也就是说dfs之前一个位置。完了以后要回溯ma[fx][fy]=1因为在其他的情况中它还是一堵墙,它没有被撞开,这个细节,叫做回溯,初学者经常会想不清楚,一定要想清楚。

    3)3:到达终点,递归。

注意点:

  首先第一个WA点:输入数据的时候,行和列的先后不一样了。  

  第二个点:很多题目中是n或m为0则终止输入,有的是n=0并且m=0才终止输入,一定要小心这个细节。

  第三个点:从样例中要观察出一个题目没有提到的细节:就是如果要用球撞破墙,那么必须球的起始点和墙必须不是相邻的。

#include <cstdio>#include <iostream>using namespace std;const int maxn = 20 + 5;const int INF = 0x3f3f3f3f;int n ,m, sx, sy, gx, gy, ans_cost;int ma[maxn][maxn];int dx[] = {-1, 1, 0, 0};int dy[] = {0, 0, -1, 1};void dfs(int x, int y, int times){    if(times > 10) return ;//这是一步剪枝    if(ma[x][y] == 3 )//即到达终点的时候 递归终点    {        if(ans_cost == -1)//如果目前为止还没有找到过解,则更新解            ans_cost = times;        else if(ans_cost != -1 && times < ans_cost)//如果已经找到过解,那么需要判断是否已经找到的解和新的解,更新解为较小者            ans_cost = times;        return ;    }    for(int i = 0; i < 4; i++)    {        int fx = x + dx[i];        int fy = y + dy[i];        if(ma[fx][fy] == 1) continue;//这是墙,如果相邻的就是墙说明无法移动,跳过此数据        while(0<= fx && fx < n && 0 <= fy && fy < m)        {            if(ma[fx][fy] == 0 || ma[fx][fy] == 2)            {                fx += dx[i];                fy += dy[i];            }            else if(ma[fx][fy] == 1)            {                ma[fx][fy] = 0;//去除障碍物                dfs(fx - dx[i], fy - dy[i], times + 1);                ma[fx][fy] = 1;//回溯                break;            }            else if(ma[fx][fy] == 3)            {                dfs(fx, fy, times + 1);                break;            }        }    }}int main(){    while(~scanf("%d%d", &m, &n))    {        if(m == 0 && n == 0) break;        for(int i = 0; i < n; i++)        {            for(int j = 0; j < m; j++)            {                scanf("%d", &ma[i][j]);                if(ma[i][j] == 2) sx = i, sy = j;            }        }        ans_cost = -1;        dfs(sx, sy, 0);        printf("%d\n", ans_cost);    }    return 0;}


 

poj 3669  Meteor Shower(bfs)

题意:有个小文青去看流星雨,不料流星掉下来会砸毁上下左右中五个点。每个流星掉下的位置和时间都不同,求小文青能否活命,如果能活命,最短的逃跑时间是多少?

思路:

  首先我们要明确,因为如果某一个点(x,y)被砸过,就从此以后再也不能走了。所以只要用ma[x][y]存下来最早砸过的时间就可以了。大于它的时间,相当于都可以舍弃了。

  其次,要明确ma[x][y]==INF的点都是安全点。

  首先是想到用预处理来使搜索的时候比较方便。不然搜索的时候还要再判断周围几个结点的流星雨的陨落的最早时间。

  所以要先做一个预处理,见代码。每一个有流星雨的点,把它和它相邻的四个结点都放进for循环里一个个去一个更新最早流星雨陨落时间。

  之后就是一个非常普通的bfs了。

注意点:

  1)特殊数据 0 0 0 要特判

  2)不知道为什么,至少在poj上,这道题。用min去更新最小值的时候,会报TLE,不论是algorithm库里的min函数还是自己写的min函数,都会超时。然后用前者的比较的形式就能够AC。

  3)bfs里面这段更新完距离之后,要记得把新结点进队。

      4)一定要注意这里的pop不能直接在取出新结点之后就pop了。想想为什么。

#include <queue>#include <cstdio>using namespace std;const int maxn = 300 + 5;const int INF = 0x3f3f3f3f;#define judge(x, y) 0 <= x  && 0 <= yint ma[maxn][maxn];int d[maxn][maxn];int dx[] = {0, 0, 1, -1, 0};int dy[] = {-1, 1, 0, 0, 0};struct Node{    int x, y;    Node(int xx, int yy):x(xx), y(yy){}};void bfs(int x, int y){    if(ma[x][y] == 0) {printf("-1\n");return ;}//特殊情况的一个判断:(0,0)在0s的时候就有陨石陨落    queue<Node>que;    Node p(x, y);    d[x][y] = 0;    que.push(p);    while(!que.empty())    {        Node newp = que.front();        int px = newp.x;        int py = newp.y;        if(ma[px][py] == INF)   break;//到达安全点,break        que.pop();//***要放在break之后        for(int i = 0; i < 4; i++)        {            int fx = px + dx[i];            int fy = py + dy[i];            if(judge(fx, fy))            {                if(ma[fx][fy] > d[newp.x][newp.y] + 1 && d[fx][fy] > d[newp.x][newp.y] + 1)                {                    d[fx][fy] = d[newp.x][newp.y] + 1;                    que.push(Node(fx, fy));                }            }        }    }    if(que.empty())//如果队空了    {        printf("-1\n");    }    else    {        Node newp = que.front();        printf("%d\n", d[newp.x][newp.y]);    }}int main(){    int M, x, y, t;    while(~scanf("%d", &M))    {        for(int i = 0; i < maxn; i++)        {            for(int j = 0; j < maxn; j++)            {                ma[i][j] = INF;//记录每个点的流星雨的最早陨落时间                d[i][j] = INF;//记录每个点的最少时间            }        }        for(int i = 0; i < M; i++)        {            scanf("%d%d%d", &x, &y, &t);            for(int j = 0; j < 5; j++)            {                int fx= x + dx[j];                int fy = y + dy[j];                if(judge(fx, fy) && ma[fx][fy] > t)                    ma[fx][fy] = t;            }        }        bfs(0, 0);    }    return 0;}



 

poj 2718  Smallest Difference(穷竭搜索,枚举)

题意:给出最多10个数字,将它们划分为两个整数,求差异值最小的值(除非只有一位数,否则不允许出现先导0)

思路:

  分析题目给的数据范围,可以发现,它可以用枚举法。怎么分析呢。

  首先最多10个数。10个数有10!,三十六万多种排列而已。也就说撑死不到四十万。而我们要求最小的差,那么说明n是偶数的时候,一定是两个位数相同的数作差,如果是奇数,说明是一个n/2和n/2+1的两个数作差。等价于它的一个排列的第一个数到p/2个数,后面组成一个数,两个数的差。枚举的话也就四十万种不到,是可以解决的。

  因为首位数不能为0,所以有一个剪枝。

  再回过头来考虑一个特殊情况,就是n=2的时候,应该要特殊处理

  再注意一个输入的方式。

#include <iostream>#include <cstdio>#include <algorithm>using namespace std;int a[15];int main(){    int T;    scanf("%d", &T);    int t;    while(T--)    {        int p = 0;        char ch;        int ans = 0x3f3f3f3f;        while(~scanf("%d%c", &a[p++], &ch))            if(ch == '\n') break;        if(p == 2)        {            cout << abs(a[0]-a[1] )<< endl;            continue;        }        do{            if(a[0] == 0 || a[p / 2] == 0) continue;//这种情况明显不可能            int A = 0, B = 0;            for(int i = 0; i < p / 2; i++)                A = A * 10 + a[i];            for(int i = p / 2; i < p; i++)                B = B * 10 + a[i];            int temp = abs(A - B);            if(temp < ans) ans = temp;        }while(next_permutation(a, a + p));        cout<< ans <<endl;    }    return 0;}



 

poj 3187  Backward Digit Sums(暴搜)

题意:给出杨辉三角的顶点值sum和底边数的个数n,求出底边各个数的值。

思路:

  要对数据范围足够敏感,n这么小,直接暴搜就行了 。暴搜全排列,模拟杨辉三角加法,看答案是否匹配。

#include <cstdio>#include <algorithm>using namespace std;int a[15], b[15];int main(){    int n, sum;    while(~scanf("%d%d", &n, &sum))    {        for(int i= 1; i <= n; i++)        {            a[i] = i;        }        do{            int len = n;            for(int i = 1; i <= n; i++)                b[i] = a[i];            while(len > 1)            {                for(int i = 1; i < len; i++)                {                    b[i] = b[i] + b[i + 1];                }                len --;            }            if(b[1] == sum)            {                for(int i = 1; i <= n; i++)                {                    printf("%d%c", a[i], i == n ? '\n' : ' ');                }                break;            }        }while(next_permutation(a + 1, a + 1 + n));    }    return 0;}


 

poj 3050  Hopscotch(暴力dfs+set/数组)

题意:

  1.5*5的方阵中,先随意挑一格,记住这个格子的数字

  2.可以上下左右走,走5次,每走一次记录下所走格子的数字

  3.经过以上步奏,把总共6个数字连起来,形成一串数字。求总共可以形成多少种不同的数字串

思路:

  思考一下状态数,一共也没多少种,直接暴力,暴力出真知。这道题贴两个代码,区别在一个是用了数组,一个是用set,一个牺牲了时间,一个牺牲了空间.

#include <iostream>#include <cstring>using namespace std;int ma[25][25];int Hash[1000000];int cnt;int dx[] = {0, 0, 1, -1};int dy[] = {1, -1, 0, 0};void solve(int x, int y, int sum, int level){    if(level == 6)    {        if(Hash[sum] == 0)            cnt++, Hash[sum] = 1;        return ;    }    for(int i = 0; i < 4; i++)    {        int fx = x + dx[i];int fy = y + dy[i];        if(0 <= fx && fx < 5 && 0 <= fy && fy < 5)        {            solve(fx, fy, sum * 10 + ma[fx][fy], level + 1);        }    }}int main(){    for(int i = 0; i < 5; i++)        for(int j = 0; j < 5; j++)            cin >> ma[i][j];    cnt = 0;    memset(Hash, 0, sizeof(Hash));    for(int i = 0; i < 5; i++)    {        for(int j = 0; j < 5; j++)        {            solve(i, j, ma[i][j], 1);        }    }    cout << cnt << endl;    return 0;}

#include <iostream>#include <set>#include <cstdio>using namespace std;int temp;set <int> s;int dx[] = {0, 0, -1, 1};int dy[] = {1, -1, 0, 0};int ma[10][10];#define judge(x, y) 0 <= x && x < 5 && 0 <= y && y < 5void solve(int x, int y, int n, int sum){    if(n == 6)    {        s.insert(sum);        return  ;    }    sum = sum * 10 + ma[x][y];    for(int i = 0; i < 4; i++)    {        int fx = x + dx[i];        int fy = y + dy[i];        if(judge(fx, fy))        {            solve(fx, fy, n + 1, sum);        }    }}int main(){    for(int i = 0; i < 5; i++)        for(int j = 0; j < 5; j++)            cin >> ma[i][j];    for(int i = 0; i < 5; i++)    {        for(int j = 0; j < 5; j++)        {            solve(i, j, 0, 0);        }    }    cout << s.size() << endl;    return 0;}


 

0 0