这个题没过……!
题意:小蚂蚁向四周走,让你在他走过的路中寻找最短路,其中可以反向
主要思路:建立想对应的图,寻找最短路径,其中错了好多次,到最后时间没过(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. Java--再次理解多态

    Java中多态性(polymorphism)的实现 什么是多态 1. 面向对象的三大特性:封装.继承.多态.从一定角度来看,封装和继承几乎都是为多态而准备的.这是我们最后一个概念,也是最重要的知识点. ...

  2. Codeforces 482 - Diverse Permutation 构造题

    这是一道蛮基础的构造题. - k         +(k - 1)      -(k - 2) 1 + k ,    1 ,         k ,             2,    ....... ...

  3. 基于Sql Server 2008的分布式数据库的实践(一)

    原文 基于Sql Server 2008的分布式数据库的实践(一) 配置Sql Server 2008(Win7) 1.打开SQL server2012,使用windows身份登录 2.登录后,右键选 ...

  4. Spring Boot 探索系列 - 自动化配置篇

    26. Logging Prev  Part IV. Spring Boot features  Next 26. Logging Spring Boot uses Commons Logging f ...

  5. 杭电--1862--EXCEL排序--结构体排序

    EXCEL排序 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  6. JAVA操作Hbase基础例子

    package com.cma.hbase.test; import java.io.BufferedInputStream; import java.io.BufferedReader; impor ...

  7. 百度编辑器ueditor简单易用

    最近刚刚被分配任务给一个新闻发布,它采用了富文本编辑器.本人也被用于,只是这一次我选择了百度的富文本编辑器ueditor1_4_3-utf8-jsp版. 事实上看ueditor功能非常强大,只是百度的 ...

  8. PHP - Cookie 应用

    效果: 代码: <?php //设置编码 header("content-type:text/html; charset=utf-8"); //接收提交的数据 //判断是否接 ...

  9. 线程:Message和Runnable

    原文地址http://blog.csdn.net/flowingflying/article/details/6370184 程序需要相应用户的操作,最要能在200ms(0.2s)之内,如果超过5秒没 ...

  10. C#中调用Windows API时的数据类型对应关系

    原文 C#中调用Windows API时的数据类型对应关系 BOOL=System.Int32 BOOLEAN=System.Int32 BYTE=System.UInt16 CHAR=System. ...