Problem E: Erratic Ants

这个题没过……!
题意:小蚂蚁向四周走,让你在他走过的路中寻找最短路,其中可以反向
主要思路:建立想对应的图,寻找最短路径,其中错了好多次,到最后时间没过(1.没有考录反向2.没有考虑走过的路要标记……!!!!!内存超了……啊啊啊啊!!!!)
总之,这样了~~

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <cctype>

const double Pi = atan(1) * 4;
using namespace std;

int graph[200][200];
bool through[100][100];
int dr[] = {1,-1,0,0};
int dc[] = {0,0,-1,1};
bool visit[200][200];
struct Point{
    int x,y;
    int step;
    Point(){
        step = 0;
    }
    Point(int xx,int yy,int tt):x(xx),y(yy),step(tt){}
};
int main()
{
    freopen("input.in","r",stdin);
    //freopen("output.in","w",stdout);
    int t;
    cin >> t;
    queue<Point>que;
    while(t--){
        int n;
        cin >> n;
        memset(graph,0,sizeof(graph));
        memset(through,0,sizeof(through));
        memset(visit,0,sizeof(visit));
        int x = 100;
        int y = 100;
        int sx = 100;
        int sy = 100;
        graph[x][y] = 1;
        char ch;
        int ww = n;
        while(n--){
            cin >> ch;
            int xx = x;
            int yy = y;
            if(ch == ‘E‘){
                xx++;
            }
            else if(ch == ‘W‘){
                xx--;
            }
            else if(ch == ‘S‘){
                yy--;
            }
            else if(ch == ‘N‘){
                yy++;
            }
            if(!graph[xx][yy])
                graph[xx][yy] = graph[x][y]+1;
            through[ graph[x][y] ][graph[xx][yy] ] = 1;
            through[ graph[xx][yy] ][graph[x][y] ] = 1;
            x = xx;
            y = yy;
        }
        if(ww == 1){
            cout << "1" << endl;
            continue;
        }
        while(!que.empty()){
            que.pop();
        }
        Point head(sx,sy,0);
        que.push(head);
        visit[sx][sy] = 1;
        while(!que.empty()){
            Point tmp = que.front();
            que.pop();
            if(tmp.x == x && tmp.y == y){
                cout << tmp.step << endl;
                break;
            }
            for(int i = 0;i < 4;i++){
                int xx = tmp.x + dr[i];
                int yy = tmp.y + dc[i];
                if(graph[xx][yy] != 0 && through[  graph[tmp.x][tmp.y] ][ graph[xx][yy] ] && !visit[xx][yy]){
                    Point tt(xx,yy,tmp.step+1);
                    que.push(tt);
                    visit[xx][yy] = 1;
                }
            }
        }
    }
    return 0;
}

时间: 2024-08-02 10:33:01

Problem E: Erratic Ants的相关文章

UVA - 10714 Ants

最多时间就是每只蚂蚁选择最久的爬行方式 最少时间就是每只蚂蚁选择最快地爬行方式 #include<iostream> #include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include&

uva 10881 Piotr&#39;s Ants 解题报告

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=20&page=show_problem&problem=1822 题目意思:有一条 L 厘米长的杆,上面有 n 只蚂蚁,给出每只蚂蚁的朝向以及离杆上最左端的距离,问 T 秒之后每只蚂蚁到达的位置,如果 T 秒后某个位置有多只蚂蚁同时到达,那么这堆蚂蚁处于的位置 + Turning,如果超过这条杆的长度,输出F

【二分匹配入门专题1】P - Ants poj2565【km----卡精度】

Young naturalist Bill studies ants in school. His ants feed on plant-louses that live on apple trees. Each ant colony needs its own apple tree to feed itself. Bill has a map with coordinates of n ant colonies and n apple trees. He knows that ants tra

UVALive 4043 Ants

KM   构图求最小权值匹配 保证最小的权值,所连的边一定是可以不相交的. Ants Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [Submit]   [Go Back]   [Status] Description Young naturalist Bill studies ants in school. His ants feed on plant-louses that live on

UVa10881 Piotr&#39;s Ants (思维)

链接:http://acm.hust.edu.cn/vjudge/problem/25979分析:n只蚂蚁在爬,将n只蚂蚁距离木棍左端的距离从小到大排序后它们的相对顺序是不变的,因为碰到另一只蚂蚁两只蚂蚁就会掉头,蚂蚁就像一个弹珠来回弹,但整体上“掉头”等价于“对穿而过”,但题目要求输入时的顺序输出,所以并不是所有蚂蚁都是一样的,还要认清谁是谁,所以如果蚂蚁1初始状态为(3,R)两秒后在(5,R)的位置会出现一只蚂蚁但不一定是蚂蚁1,但是可以肯定(5,R)位置上的蚂蚁相对于其它蚂蚁的顺序和初始状

ACM学习历程—POJ3565 Ants(最佳匹配KM算法)

Young naturalist Bill studies ants in school. His ants feed on plant-louses that live on apple trees. Each ant colony needs its own apple tree to feed itself. Bill has a map with coordinates of n ant colonies and n apple trees. He knows that ants tra

UVA 10881 - Piotr&#39;s Ants【模拟+思维】

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1822 题意:有很多只蚂蚁在一条直线上,每个蚂蚁移动速度都是1,并且有一个初始方向.并且当相邻两个蚂蚁相撞时转向.现在问t时间后各个蚂蚁的位置. 解法:这题的一个致命技巧就是把两只蚂蚁的相撞看作是两只蚂蚁交换穿过对方并且交换蚂蚁的编号.这个是很好理解的,类似于物理的完全弹性碰撞.又由

POJ 3565 Ants

Ants Time Limit: 5000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 356564-bit integer IO format: %lld      Java class name: Main Young naturalist Bill studies ants in school. His ants feed on plant-louses that live on appl

UVA12709 Falling Ants(超级大水题)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4447 Ps:此题超级水,但是光看题目就会被吓一跳.确实题目很长,不过有用的语句少之又少,介绍一大堆与AC没有半毛钱关系的东西,汗颜. 此题告诉我们题目长不一定是最难得题,反而有时候是最简单的题,就看你有