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\n", 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\n", success);
        else
            printf("-1\n");
    }
    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\n", 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\n");
        else
            printf("YES\n");
    }
    return 0;
}

 

posted @ 2017-07-18 05:45  可达龙  阅读(355)  评论(0编辑  收藏  举报