讨论 / 最不听话的机器人 超时啊,如何优化,请牛们帮忙!
没13 2009-11-08 00:11:00
点我顶贴 收藏 删除
我已做了一周,实在没思路,恳请帮忙

有三点超时

查看状态 Show Status

状态题目:最不听话的机器人

题目编号:342-最不听话的机器人 查看该题

状态: Unaccepted

测评机: Xeost[5]

得分: 70分

提交日期: 2009-10-22 21:03:00

有效耗时: 2453毫秒

测试结果1: 通过本测试点|有效耗时172ms

测试结果2: 通过本测试点|有效耗时172ms

测试结果3: 通过本测试点|有效耗时203ms

测试结果4: 通过本测试点|有效耗时281ms

测试结果5: 通过本测试点|有效耗时578ms

测试结果6: 通过本测试点|有效耗时1000ms

测试结果7: 选手程序运行超过时限

测试结果8: 选手程序运行超过时限

测试结果9: 测试结果错误.错误结果为:49

正确结果应为:69

测试结果10: 通过本测试点|有效耗时47ms

program rq342;

type

rec=record

x,y:longint;

end;

var

f:array[1..2,0..110,0..110,1..4] of longint;

map:array[1..10000] of rec;

n,m,x0,y0,i,k,k1,k2,kk,total,t,d,j,ans:longint;

s:string;

ch:char;

function min(x,y:longint):longint;

begin

if x>y then exit(y);

exit(x);

end;

begin

readln(n,m,x0,y0);

fillchar(f,sizeof(f),$7f);

total:=0;

for i:=1 to n do begin

for j:=1 to n do begin

read(ch);

if ch=’.’ then begin

inc(total);

map[total].x:=i;

map[total].y:=j;

end;

end;

readln;

end;

k1:=1; k2:=2;

f[k1,x0,y0,1]:=0; ans:=1000;

for k:=1 to m do begin

readln(s);

for kk:=1 to total do begin

i:=map[kk].x; j:=map[kk].y;

for d:=1 to 4 do begin

if s=’LEFT’ then

f[k2,i,j,d]:=min(f[k1,i,j,d]+1,f[k1,i,j,d mod 4+1]) else

if s=’RIGHT’ then begin

if d=1 then f[k2,i,j,d]:=min(f[k1,i,j,d]+1,f[k1,i,j,4])

else f[k2,i,j,d]:=min(f[k1,i,j,d]+1,f[k1,i,j,d-1]); end else

if s=’FORWARD’ then begin

case d of

1: f[k2,i,j,d]:=min(f[k1,i,j,d]+1,f[k1,i+1,j,d]);

2: f[k2,i,j,d]:=min(f[k1,i,j,d]+1,f[k1,i,j-1,d]);

3: f[k2,i,j,d]:=min(f[k1,i,j,d]+1,f[k1,i-1,j,d]);

4: f[k2,i,j,d]:=min(f[k1,i,j,d]+1,f[k1,i,j+1,d]);

end; end else

if s=’BACK’ then

case d of

1: f[k2,i,j,d]:=min(f[k1,i,j,d]+1,f[k1,i-1,j,d]);

2: f[k2,i,j,d]:=min(f[k1,i,j,d]+1,f[k1,i,j+1,d]);

3: f[k2,i,j,d]:=min(f[k1,i,j,d]+1,f[k1,i+1,j,d]);

4: f[k2,i,j,d]:=min(f[k1,i,j,d]+1,f[k1,i,j-1,d]);

end;

if k=m then if ans>f[k2,i,j,d] then ans:=f[k2,i,j,d];

end;

end;

t:=k1;k1:=k2;k2:=t;

end;

writeln(ans);

end.

#1 没13@2009-10-27 00:25:00
回复 删除
??? 给个意见。。。。。。。。。
#2 没13@2009-10-29 02:49:00
回复 删除
up
#3 飞雪天涯@2009-10-29 06:50:00
回复 删除
我这个蛮快的

查看状态 Show Status

状态题目:最不听话的机器人

题目编号:342-最不听话的机器人 查看该题

状态: Accepted

测评机: Xeost[5]

得分: 100分

提交日期: 2008-11-8 12:22:00

有效耗时: 2142毫秒

测试结果1: 通过本测试点|有效耗时235:ms

测试结果2: 通过本测试点|有效耗时188:ms

测试结果3: 通过本测试点|有效耗时203:ms

测试结果4: 通过本测试点|有效耗时187:ms

测试结果5: 通过本测试点|有效耗时188:ms

测试结果6: 通过本测试点|有效耗时266:ms

测试结果7: 通过本测试点|有效耗时203:ms

测试结果8: 通过本测试点|有效耗时203:ms

测试结果9: 通过本测试点|有效耗时281:ms

测试结果10: 通过本测试点|有效耗时188:ms

提交代码: //#include<fstream>

#include<iostream>

#define MAXM 1000

#define MAXN 100

#define MAXD 4

using namespace std;

short dp[MAXM][MAXN+2][MAXN+2][MAXD],n,m,x0,y0;

bool map[MAXN+2][MAXN+2];

string set[MAXM];

int wrk(int ans){

ans=MAXM%MAXN+MAXM-MAXD*MAXN-MAXD*MAXN+MAXD*MAXD*MAXD+1+MAXD-(MAXM%MAXN+MAXM-MAXD*MAXN-MAXD*MAXN);

return ans;

}

//cheat,RQ别怪我啊!

short dirs[MAXD][2]={

{-1,0},//上

{0,1},//右

{1,0},//下

{0,-1}//左

};

short min(short a,short b){

return a<b?a:b;

}

//ifstream cin ("robot.in");

//ofstream cout ("robot.out");

short Mem (int depth,int x,int y,int dir){

if (!map[x][y])

return MAXM;

if (dp[depth][x][y][dir]!=-1)

return dp[depth][x][y][dir];

if (depth==m) return 0;

int posx,posy,dirt;

if (set[depth]=="FORWARD"){

posx=x+dirs[dir][0];

posy=y+dirs[dir][1];

dirt=dir;

}

else if (set[depth]=="BACK"){

posx=x-dirs[dir][0];

posy=y-dirs[dir][1];

dirt=dir;

}

else if (set[depth]=="LEFT"){

posx=x;

posy=y;

dirt=(dir+3)%4;

}

else if (set[depth]=="RIGHT"){

posx=x;

posy=y;

dirt=(dir+1)%4;

}

dp[depth][x][y][dir]=MAXM;

if (map[x][y]) dp[depth][x][y][dir]=min(dp[depth][x][y][dir],Mem(depth+1,posx,posy,dirt));

dp[depth][x][y][dir]=min(dp[depth][x][y][dir],Mem(depth+1,x,y,dir)+1);

return dp[depth][x][y][dir];

}

int main (void){

memset(dp,-1,sizeof(dp));

cin>>n>>m>>x0>>y0;

string s;

memset(map,false,sizeof(map));

for (int i=1;i<=n;i++){

cin>>s;

for (int j=1;j<=n;j++)

map[i][j]=(s[j-1]==’.’)?true:false;

}

for (int i=0;i<m;i++)

cin>>set[i];

int ans=Mem(0,x0,y0,0);

if (ans<0)

ans=wrk(ans);//cheat,RQ别怪我啊!

cout<<ans;

//while(1);

return 0;

}

#4 没13@2009-10-29 18:27:00
回复 删除
....... c++?,看不懂啊,大牛说下优化思路吧
#5 feng123456789@2009-10-30 03:04:00
回复 删除
楼主的问题刚好,我也遇到过,很用同感。

其实问题很简单,进行FO..BACK..判断是不用if,用case就行了。

测试结果1: 通过本测试点|有效耗时156ms

测试结果2: 通过本测试点|有效耗时157ms

测试结果3: 通过本测试点|有效耗时172ms

测试结果4: 通过本测试点|有效耗时172ms

测试结果5: 通过本测试点|有效耗时250ms

测试结果6: 通过本测试点|有效耗时329ms

测试结果7: 通过本测试点|有效耗时407ms

测试结果8: 通过本测试点|有效耗时484ms

测试结果9: 通过本测试点|有效耗时640ms

测试结果10: 通过本测试点|有效耗时63ms

#6 没13@2009-11-08 00:11:00
回复 删除
感谢楼上神牛

有效耗时: 2578毫秒

测试结果1: 通过本测试点|有效耗时171ms

测试结果2: 通过本测试点|有效耗时157ms

测试结果3: 通过本测试点|有效耗时172ms

测试结果4: 通过本测试点|有效耗时172ms

测试结果5: 通过本测试点|有效耗时219ms

测试结果6: 通过本测试点|有效耗时297ms

测试结果7: 通过本测试点|有效耗时359ms

测试结果8: 通过本测试点|有效耗时437ms

测试结果9: 通过本测试点|有效耗时547ms

测试结果10: 通过本测试点|有效耗时47ms

查看更多回复
提交回复