zoukankan      html  css  js  c++  java
  • POJ 1979 POJ 3009 AOJ 0033 AOJ 0118 [搜索类题目][0033贪心模拟]

    /**
    POJ 1979
    
    BFS
    */
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <queue>
    using namespace std;
    const int N = 20 + 5;
    
    int mp[N][N];
    int sx,sy;
    int n, m;
    int vis[3000];
    int dirx[] = {0, 1, 0, -1};
    int diry[] = {1, 0, -1, 0};
    void solve()
    {
        queue<int> Q;
        int cnt = 1;
        memset(vis, 0 , sizeof(vis));
        while(Q.size()) Q.pop();
        Q.push(sx*100 + sy);
        vis[sx*100 + sy] = 1;
        while(!Q.empty())
        {
            int tmp = Q.front(); Q.pop();
            int x = tmp / 100, y = tmp % 100;
            for(int i = 0; i < 4; i++)
            {
                int nx = x + dirx[i], ny = y + diry[i];
                if(nx < 0 || nx >= m || ny < 0 || ny >= n || vis[nx*100 + ny] || mp[nx][ny] == 0) continue;
                vis[nx*100+ny] = 1;
                cnt++;
                Q.push(nx*100 + ny);
            }
        }
        printf("%d
    ", cnt);
    }
    
    int main()
    {
        while(scanf("%d%d", &n, &m), n || m)
        {
            getchar();
            char ch;
            for(int i = 0; i < m; i++)
            {
                for(int j = 0; j < n; j++)
                {
                    scanf("%c", &ch);
                    if(ch == '.')
                        mp[i][j] = 1;
                    else if(ch == '#')
                        mp[i][j] = 0;
                    else if(ch == '@')
                    {
                        sx = i, sy = j;
                        mp[i][j] = 1;
                    }
                }
                getchar();
            }
            solve();
        }
        return 0;
    }
    /**
    POJ 3009
    
    DFS
    */
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    using namespace std;
    const int N = 20 + 5;
    const int INF = 0x3f3f3f3f;
    int n, m;
    int mp[N][N];
    int dirx[] = {0, -1, 0, 1};
    int diry[] = {1, 0, -1, 0};
    int sx,sy,ex,ey;
    int success;
    
    int dfs(int x, int y, int cur, int step)
    {
        if(cur >= 10)
            return 0;
        for(int i = 0; i < 4; i++)
        {
            int nx = x + dirx[i], ny = y + diry[i];
            if(nx < 0|| nx >= m || ny < 0 || ny >= n || mp[nx][ny] == 1) continue;
            if(nx == ex && ny == ey)
            {
                success = min(success, step + 1);
                return 1;
            }
            while(nx >= 0 && nx < m && ny >= 0 && ny < n
                  && mp[nx][ny] == 0)
            {
                nx += dirx[i];
                ny += diry[i];
                if(nx == ex && ny == ey)
                {
                    success = min(success, step + 1);
                    return 1;
                }
            }
            if(nx < 0 || nx >=m || ny < 0 || ny >= n ) continue;
            if(mp[nx][ny] == 1)
            {
                mp[nx][ny] = 0;
                nx -= dirx[i];
                ny -= diry[i];
                dfs(nx, ny, cur+1, step+1);
                nx += dirx[i];
                ny += diry[i];
                mp[nx][ny] = 1;
            }
        }
        return 0;
    }
    
    
    int main()
    {
        while(scanf("%d%d", &n, &m), n || m)
        {
            for(int i = 0; i < m; i++)
            {
                for(int j = 0; j < n; j++)
                {
                    scanf("%d", &mp[i][j]);
                    if(mp[i][j] == 2)
                    {
                        sx = i, sy = j;
                        mp[i][j] = 0;
                    }
                    else if(mp[i][j] == 3)
                    {
                        ex = i, ey = j;
                        mp[i][j] = 0;
                    }
                }
            }
            success = INF;
            dfs(sx,sy, 0, 0);
    
            if(success < INF)
                printf("%d
    ", success);
            else
                printf("-1
    ");
        }
        return 0;
    }
    /**
    AOJ 0118
    
    DFS
    */
    
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    using namespace std;
    const int N = 100 + 5;
    
    int n,m;
    int mp[N][N];
    int dirx[] = {0, 1, 0, -1};
    int diry[] = {1, 0, -1, 0};
    int apple, pear, orange;
    
    void dfs(int x, int y, int color)
    {
        mp[x][y] = 0;
        for(int i = 0; i < 4; i++)
        {
            int nx = x + dirx[i], ny = y + diry[i];
            if(nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
            if(mp[nx][ny] == color)
                dfs(nx, ny, color);
        }
    }
    
    
    int main()
    {
        while(scanf("%d%d", &n, &m), n || m)
        {
            apple = pear = orange = 0;
            char ch;
            getchar();
            for(int i = 0; i < n; i++)
            {
                for(int j = 0; j < m; j++)
                {
                    scanf("%c", &ch);
                    if(ch == '@') mp[i][j] = 1;
                    else if(ch == '#') mp[i][j] = 2;
                    else if(ch == '*') mp[i][j] = 3;
                }
                getchar();
            }
    
            for(int i = 0; i < n; i++)
            {
                for(int j = 0; j < m; j++)
                {
                    if(mp[i][j] == 1)
                    {
                        apple++;
                    }
                    else if(mp[i][j] == 2)
                    {
                        pear++;
                    }
                    else if(mp[i][j] == 3)
                    {
                        orange++;
                    }
                    else
                        continue;
                    dfs(i, j, mp[i][j]);
                }
            }
            printf("%d
    ", apple+pear+orange);
        }
        return 0;
    }
    /**
    贪心模拟
    
    先放左再放右,优先选择大的能放的放
    */
    
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    using namespace std;
    const int N = 20 + 5;
    
    int T;
    
    int main()
    {
        scanf("%d", &T);
        while(T--)
        {
            int left = 0, right = 0, flag = 0;
            for(int i = 0; i < 10; i++)
            {
                int tmp;
                scanf("%d", &tmp);
                if(tmp > left) left =tmp;
                else if(tmp > right) right = tmp;
                else flag = 1;
            }
            if(flag)
                printf("NO
    ");
            else
                printf("YES
    ");
        }
        return 0;
    }
    如果有错误,请指出,谢谢
  • 相关阅读:
    Java反射研究(3)
    Java反射研究(2)
    Java反射研究(1)
    从百度文库中复制文本的方法
    jdk/bin中的native2ascii.exe使用
    EA经典入门教程
    JSP自定义标签(3)
    JSP自定义标签(1)
    将Java doc转换为chm的方法
    多态性——vptr和vtable
  • 原文地址:https://www.cnblogs.com/Alruddy/p/7198464.html
Copyright © 2011-2022 走看看