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

    C# 7.0 本文参考Roslyn项目中的Issue:#118. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# 7.0 新特性 ...

  2. IEEE浮点数float、double的存储结构

    众所周知,C的float.VB的Single都是32位浮点数变量类型(也叫单精度浮点数),C的double和VB的Double则都是64位的浮点数变量类型(也叫双精度浮点数).有些编译器还支持更屌的l ...

  3. 由动态库文件dll生成lib库文件(手动生成.def文件,然后使用lib命令编译,非常牛),同理可使用dll生成.a库文件

    本文基于OpenBlas的编译和安装,来说明如何从一个dll文件生成lib库文件. 参考OpenBlas的说明“Howto generate import library for MingW”,和Mi ...

  4. larbin是一种开源的网络爬虫/网络蜘

    larbin是一种开源的网络爬虫/网络蜘蛛,由法国的年轻人 Sébastien Ailleret独立开发.larbin目的是能够跟踪页面的url进行扩展的抓取,最后为搜索引擎提供广泛的数据来源.Lar ...

  5. 在 vb中 "end","unload me","exit sub" 之间的区别

    之前就想过这个问题,这么熟悉的几个东西居然对他们分析的不是很透彻. “End”  跟  “Unload  Me”  在敲程序 的时候经常敲到,“exit  sub”  更是熟悉,下面,解析: End  ...

  6. WPF实现界面动态布局

    曾经总认为动态布局是个非常麻烦的问题.是个非常须要功力的问题.可是貌似在.NET中,在WPF中却不是那么的麻烦.以下介绍我如今实现的一个动态布局的实例. 由于有需求,所以困难得克服!而我们的需求表名. ...

  7. InstallShield安装包卸载-完美卸载

    在前面的日志里面介绍了在卸载的时候删除整个安装文件夹的方式.可是当遇到程序生成的文件不是在同一个文件夹下,有时甚至是用户自己定义的文件夹路径,这个时候我们卸载的时候是没有将用户自己定义的文件夹给删除掉 ...

  8. C++多态原理

    C++的多态性是通过动态绑定实现的 非虚函数是在编译时绑定的; 通过对象进行的函数(虚函数,非虚函数)也是编译时绑定的; C++编译器在编译的时候,要确定每个对象调用的函数(要求此函数是非虚函数)的地 ...

  9. day8 - isinstance--issubclass-异常处理-自定义异常

    一.isinstance(obj, cls) 检查是否obj是否是类 cls 的对象 __author__ = 'Administrator' class Foo(object): pass obj ...

  10. session临时文件存储路径

    今天把本地的项目部署到外网时,遇到个问题,使用session_start()时,提示****目录下的这个文件不存在,网上查了查,说我的根目录不存在,然后在php.ini文件里看了下session.sa ...