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

    本网格布局系统属于Scaffolding(框架,布局)部分.在Scaffolding里面有(固定)网格布局(Grid System)和流式网格布局(Fluid Grid System).本文讨论第一种 ...

  2. 基于html5 localStorage , web SQL, websocket的简单聊天程序

    new function() { var ws = null; var connected = false; var serverUrl; var connectionStatus; var send ...

  3. 再造 “手机QQ” 侧滑菜单(一)——实现侧滑效果

    本系列文章中,我们将尝试再造手机QQ的侧滑菜单,力争最大限度接近手Q的实际效果,并使用 Auto Layout 仿造左侧菜单,实现和主视图的联动. 代码示例:https://github.com/jo ...

  4. 2008r2 显示桌面图标

  5. Repeater控件

    一:http://www.cnblogs.com/foolin/archive/2011/08/31/2161342.html 二:http://www.cnblogs.com/shipfi/arch ...

  6. Image控件

    前台代码: <asp:Image ID="Image1" runat="server" /> 后台代码; protected void Page_L ...

  7. webform--常用的控件

    一.简单控件 1.Lable——标签:在网页中呈现出来的时候会变成span标签 属性:Text——标签上的文字  BackColor,ForeColor——背景色,前景色 Font——字体 Bold- ...

  8. javascript重点笔记

    操作符之间的优先级(高到低):算术操作符 >比较操作符 >逻辑操作符 >"="赋值符号 算术运算符

  9. Android 修改屏幕解锁方式

    Android 修改屏幕解锁方式 问题 在手机第一次开机的时候,运行手机激活的APP 在激活APP允许过程中,当用户按电源键的时候,屏幕黑掉,进入锁屏状态 手机默认的锁屏是滑动解锁 用户这个时候再一次 ...

  10. mysql 结合keepalived测试

    vip:192.168.32.66 192.168.32.6 主库: mysql> show variables like '%read_only%'; +------------------+ ...