这个题没过……!
题意:小蚂蚁向四周走,让你在他走过的路中寻找最短路,其中可以反向
主要思路:建立想对应的图,寻找最短路径,其中错了好多次,到最后时间没过(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() * ;
using namespace std; int graph[][];
bool through[][];
int dr[] = {,-,,};
int dc[] = {,,-,};
bool visit[][];
struct Point{
int x,y;
int step;
Point(){
step = ;
}
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,,sizeof(graph));
memset(through,,sizeof(through));
memset(visit,,sizeof(visit));
int x = ;
int y = ;
int sx = ;
int sy = ;
graph[x][y] = ;
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]+;
through[ graph[x][y] ][graph[xx][yy] ] = ;
through[ graph[xx][yy] ][graph[x][y] ] = ;
x = xx;
y = yy;
}
if(ww == ){
cout << "" << endl;
continue;
}
while(!que.empty()){
que.pop();
}
Point head(sx,sy,);
que.push(head);
visit[sx][sy] = ;
while(!que.empty()){
Point tmp = que.front();
que.pop();
if(tmp.x == x && tmp.y == y){
cout << tmp.step << endl;
break;
}
for(int i = ;i < ;i++){
int xx = tmp.x + dr[i];
int yy = tmp.y + dc[i];
if(graph[xx][yy] != && through[ graph[tmp.x][tmp.y] ][ graph[xx][yy] ] && !visit[xx][yy]){
Point tt(xx,yy,tmp.step+);
que.push(tt);
visit[xx][yy] = ;
}
}
}
}
return ;
}

Problem E: Erratic Ants的更多相关文章

  1. POJ 3565 Ants 【最小权值匹配应用】

    传送门:http://poj.org/problem?id=3565 Ants Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: ...

  2. ProgrammingContestChallengeBook

    POJ 1852 Ants POJ 2386 Lake Counting POJ 1979 Red and Black AOJ 0118 Property Distribution AOJ 0333 ...

  3. Uva---10881 Piotr's Ants(蚂蚁)

    Problem DPiotr's AntsTime Limit: 2 seconds "One thing is for certain: there is no stopping them ...

  4. Educational Codeforces Round 7 E. Ants in Leaves 贪心

    E. Ants in Leaves 题目连接: http://www.codeforces.com/contest/622/problem/E Description Tree is a connec ...

  5. Codeforces Round #188 (Div. 1) B. Ants 暴力

    B. Ants Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/317/problem/B Des ...

  6. 【UVA 1411】 Ants (KM)

    Young naturalist Bill studies ants in school. His ants feed onplant-louses that live on apple trees. ...

  7. UVALive 4043 Ants

    KM   构图求最小权值匹配 保证最小的权值,所连的边一定是能够不相交的. Ants Time Limit: 3000MS   Memory Limit: Unknown   64bit IO For ...

  8. 【巧妙的模拟】【UVA 10881】 - Piotr's Ants/Piotr的蚂蚁

    </pre></center><center style="font-family: Simsun;font-size:14px;"><s ...

  9. 10881 - Piotr's Ants

    Problem D Piotr's Ants Time Limit: 2 seconds "One thing is for certain: there is no stopping th ...

随机推荐

  1. HDU 5012 Dice DFS

    简单DFS //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h ...

  2. (Problem 4)Largest palindrome product

    A palindromic number reads the same both ways. The largest palindrome made from the product of two 2 ...

  3. DE1-SOC的sof文件无法下载解决办法

    按照前面友晶科技的文档介绍一步步的做,后面发现根本无法下载sof文件. 通常思维: 一,器件选错了.器件选择错误会导致sof文件无法下载,至于这个ID具体怎么识别我没有追究.如果是这种错误Quartu ...

  4. 上海投行需要一大群JAVA,C++,C#,UNIX.走过路过不要错过!过完年想换工作看过来初级资深都有 - V2EX

    上海投行需要一大群JAVA,C++,C#,UNIX.走过路过不要错过!过完年想换工作看过来初级资深都有 - V2EX 上海投行需要一大群JAVA,C++,C#,UNIX.走过路过不要错过!过完年想换工 ...

  5. OGR SQL

    The OGRDataSource supports executing commands against a datasource via the OGRDataSource::ExecuteSQL ...

  6. 以libevent网络库为引:网络通信和多线程

    1. windows下编译及使用libevent  http://www.cnblogs.com/luxiaoxun/p/3603399.html 2.  <<libevent学习资料&g ...

  7. asp.net用Zxing库实现条形码输出

    原文 asp.net用Zxing库实现条形码输出 这个简单的问题,困扰了我将近两个小时,主要是再官方文档上没有找到demo,那只能用搜索引擎了,看看其他程序员写的程序,但是发现处处是坑,看了四五个例子 ...

  8. Oracle多实例的配置方法

    SID_LIST_LISTENER = (SID_LIST = (SID_DESC = (SID_NAME = PLSExtProc) (ORACLE_HOME /dbhome_2) (PROGRAM ...

  9. linux系统文件属性-硬连接、软连接

    1 硬链接概念 硬链接是指通过索引节点(Inode)来进行链接,在Linux(ext2,ext3)文件系统中,保存在磁盘分区中的文件不管是什么类型都会给它分配一个编号,这个编号被称为索引节点编号(In ...

  10. servlet 与缓存(4)

    1.设置缓存的两种情况: 第一种:对于不常常变化的数据,在servlet中能够为其设置合理的缓存时间值,以避免浏览器频繁向server发送请求,提升server的性能. 另外一种:假设要实现一种高级功 ...