poj 3083 TLE

来源:互联网 发布:画谱子软件 编辑:程序博客网 时间:2024/04/28 12:48

提交了N次,总是TLE,疯了。。

以后再看看吧。

#include <iostream>#include <sstream>#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <vector>#include <set>#include <cctype>#include <algorithm>#include <cmath>#include <deque>#include <queue>#include <map>#include <queue>#include <list>#include <iomanip>using namespace std;                                               //////#define INF 0xffffff7#define maxn 50#define max(a,b)(a>b?a:b)///int ncases;int width, height;char maze[maxn][maxn];int color[maxn][maxn];int minSteps, leftSteps, rightSteps;struct pos{int y;int x;int dir;};int dir;//初始方向int dy[] = {-1,  0, 1, 0};int dx[] = { 0, -1, 0, 1};//以方向向上的时候的delta变化量int dy_left[] = { 0, -1, 0, 1};int dx_left[] = {-1,  0, 1, 0};int dy_right[] = {0, -1,  0, 1};int dx_right[] = {1,  0, -1, 0};//根据当前方向进行delta时需要旋转的量int Lrotate[] = {-1, 0, 1, 2};int Rrotate[] = { 1, 0, -1, 2};queue<pos> stk;pos startpos, targetpos;void bfs(){int i, j;int hp = 0, tp = 1, lc = 1;while(!stk.empty())stk.pop();memset(color, 0, sizeof(color));color[startpos.y][startpos.x] = 1;stk.push(startpos);minSteps = 1;while (!stk.empty()){pos tmp, newpos;tmp = stk.front();stk.pop();hp++;if (tmp.y == targetpos.y && tmp.x == targetpos.x)return;for (i = 0; i < 4; i++){newpos.y = tmp.y + dy[i];newpos.x = tmp.x + dx[i];if (newpos.y >= 1 && newpos.y <= height && newpos.x >= 1 && newpos.x <= width && !color[newpos.y][newpos.x] && maze[newpos.y][newpos.x] != '#'){color[newpos.y][newpos.x] = 1;stk.push(newpos);tp++;}}if (hp == lc){minSteps++;lc = tp;}}}bool finish_left, finish_right;void dfs_left(pos cur, int dir){int i, j;if (finish_left)return;if (cur.y == targetpos.y && cur.x == targetpos.x){finish_left = true;return;}for (i = 0; i < 4; i++){pos newpos;newpos.y = cur.y + dy_left[(i + dir) % 4];newpos.x = cur.x + dx_left[(i + dir) % 4];int dir2 = (dir + Lrotate[i] + 4) % 4;if (newpos.y >= 1 && newpos.y <= height && newpos.x >= 1 && newpos.x <= width && !color[newpos.y][newpos.x] && maze[newpos.y][newpos.x] != '#'){color[newpos.y][newpos.x] = 1;leftSteps++;dfs_left(newpos, dir2);if (!finish_left)leftSteps++;elsereturn;color[newpos.y][newpos.x] = 0;}}}void dfs_rifht(pos cur, int dir){int i, j;if (finish_right)return;if (cur.y == targetpos.y && cur.x == targetpos.x){finish_right = true;return;}for (i = 0; i < 4; i++){pos newpos;newpos.y = cur.y + dy_right[(i + dir) % 4];newpos.x = cur.x + dx_right[(i + dir) % 4];int dir2 = (dir + Rrotate[i] + 4) % 4;if (newpos.y >= 1 && newpos.y <= height && newpos.x >= 1 && newpos.x <= width && !color[newpos.y][newpos.x] && maze[newpos.y][newpos.x] != '#'){color[newpos.y][newpos.x] = 1;rightSteps++;dfs_rifht(newpos, dir2);if (!finish_right)rightSteps++;elsereturn;color[newpos.y][newpos.x] = 0;}}}int main(){///int i, j, k;cin >> ncases;while (ncases--){memset(maze, 0, sizeof(maze));cin >> width >> height;for (i = 1; i <= height; i++){for (j = 1; j <= width; j++){cin >> maze[i][j];if (maze[i][j] == 'S'){startpos.y = i;startpos.x = j;startpos.dir = 1;}if (maze[i][j] == 'E'){targetpos.y = i;targetpos.x = j;}}}//确定初始方向if(startpos.x == 1) dir = 1; //右else if(startpos.x == width) dir = 3; //左else if(startpos.y == 1) dir = 2; //下else if(startpos.y == height)             dir = 0;//上 //dfs求靠最左搜索的结果leftSteps = 1;memset(color, 0, sizeof(color));finish_left = false;color[startpos.y][startpos.x] = 1;dfs_left(startpos, dir);//dfs求靠最右搜索的结果rightSteps = 1;memset(color, 0, sizeof(color));finish_right = false;color[startpos.y][startpos.x] = 1;dfs_rifht(startpos, dir);//bfs求最短路径bfs();cout << leftSteps << " " << rightSteps << " " << minSteps << endl;}///    return 0;}                                              


 改了下跟方向有关的操作,依然TLE

#include <iostream>#include <sstream>#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <vector>#include <set>#include <cctype>#include <algorithm>#include <cmath>#include <deque>#include <queue>#include <map>#include <queue>#include <list>#include <iomanip>using namespace std;                                               //////#define INF 0xffffff7#define maxn 50#define max(a,b)(a>b?a:b)///int ncases;int width, height;char maze[maxn][maxn];int color[maxn][maxn];int minSteps, leftSteps, rightSteps;struct pos{int y;int x;int dir;};int dir;//初始方向int dy[] = {-1,  0, 1, 0};int dx[] = { 0, -1, 0, 1};//int dirL[4][2] = { {0,1}, {1,0}, {0,-1}, {-1,0} };  int dirR[4][2] = { {0,1}, {-1,0}, {0,-1}, {1,0} };//以方向向上的时候的delta变化量int dy_left[] = { 0, -1, 0, 1};int dx_left[] = {-1,  0, 1, 0};int dy_right[] = {0, -1,  0, 1};int dx_right[] = {1,  0, -1, 0};//根据当前方向进行delta时需要旋转的量int Lrotate[] = {-1, 0, 1, 2};int Rrotate[] = { 1, 0, -1, 2};queue<pos> stk;pos startpos, targetpos;void bfs(){int i, j;int hp = 0, tp = 1, lc = 1;while(!stk.empty())stk.pop();memset(color, 0, sizeof(color));color[startpos.y][startpos.x] = 1;stk.push(startpos);minSteps = 1;while (!stk.empty()){pos tmp, newpos;tmp = stk.front();stk.pop();hp++;if (tmp.y == targetpos.y && tmp.x == targetpos.x)return;for (i = 0; i < 4; i++){newpos.y = tmp.y + dy[i];newpos.x = tmp.x + dx[i];if (newpos.y >= 1 && newpos.y <= height && newpos.x >= 1 && newpos.x <= width && !color[newpos.y][newpos.x] && maze[newpos.y][newpos.x] != '#'){color[newpos.y][newpos.x] = 1;stk.push(newpos);tp++;}}if (hp == lc){minSteps++;lc = tp;}}}bool finish_left, finish_right;void dfs_left(pos cur, int dir){int i, j, dir2;if (finish_left)return;if (cur.y == targetpos.y && cur.x == targetpos.x){finish_left = true;return;}for (i = 0; i < 4; i++){int t = (dir + i) % 4;pos newpos;//newpos.y = cur.y + dy_left[(i + dir) % 4];//newpos.x = cur.x + dx_left[(i + dir) % 4];//dir2 = (dir + Lrotate[i] + 4) % 4;newpos.y = cur.y + dirL[t][0];newpos.x = cur.x + dirL[t][1];dir2 = (t+3) % 4;if (newpos.y >= 1 && newpos.y <= height && newpos.x >= 1 && newpos.x <= width && !color[newpos.y][newpos.x] && maze[newpos.y][newpos.x] != '#'){color[newpos.y][newpos.x] = 1;leftSteps++;dfs_left(newpos, dir2);if (!finish_left)leftSteps++;elsereturn;color[newpos.y][newpos.x] = 0;}}}void dfs_rifht(pos cur, int dir){int i, j, dir2;if (finish_right)return;if (cur.y == targetpos.y && cur.x == targetpos.x){finish_right = true;return;}for (i = 0; i < 4; i++){int t = (dir + i) % 4;pos newpos;//newpos.y = cur.y + dy_right[(i + dir) % 4];//newpos.x = cur.x + dx_right[(i + dir) % 4];//dir2 = (dir + Rrotate[i] + 4) % 4;newpos.y = cur.y + dirR[t][0];newpos.x = cur.x + dirR[t][1];dir2 = (t+3) % 4;if (newpos.y >= 1 && newpos.y <= height && newpos.x >= 1 && newpos.x <= width && !color[newpos.y][newpos.x] && maze[newpos.y][newpos.x] != '#'){color[newpos.y][newpos.x] = 1;rightSteps++;dfs_rifht(newpos, dir2);if (!finish_right)rightSteps++;elsereturn;color[newpos.y][newpos.x] = 0;}}}int main(){///int i, j, k;cin >> ncases;while (ncases--){memset(maze, 0, sizeof(maze));cin >> width >> height;for (i = 1; i <= height; i++){for (j = 1; j <= width; j++){cin >> maze[i][j];if (maze[i][j] == 'S'){startpos.y = i;startpos.x = j;startpos.dir = 1;}if (maze[i][j] == 'E'){targetpos.y = i;targetpos.x = j;}}}//确定初始方向if(startpos.x == 1) dir = 1; //右else if(startpos.x == width) dir = 3; //左else if(startpos.y == 1) dir = 0; //下else if(startpos.y == height)             dir = 2;//上 //dfs求靠最左搜索的结果leftSteps = 1;memset(color, 0, sizeof(color));finish_left = false;color[startpos.y][startpos.x] = 1;dfs_left(startpos, dir);//dfs求靠最右搜索的结果rightSteps = 1;memset(color, 0, sizeof(color));finish_right = false;color[startpos.y][startpos.x] = 1;dfs_rifht(startpos, dir);//bfs求最短路径bfs();cout << leftSteps << " " << rightSteps << " " << minSteps << endl;}///    return 0;}                                              


贴一个别人写的比较好的(似乎没有看到差很多,但是我的就是TLE呢??)

#include <queue>#include <iostream>using namespace std;char Maze[44][44];int dirL[4][2] = { {0,1}, {1,0}, {0,-1}, {-1,0} };int dirR[4][2] = { {0,1}, {-1,0}, {0,-1}, {1,0} };int sr, sc, er, ec, w, h;bool vis[44][44];struct Point{int r, c, step;} node[42][42];int dfs ( int r, int c, int way, int dir[][2] ){if ( r == er && c == ec )return 1;int t, a, b;for ( int i = 0; i < 4 ; i++ ){t = ( way + i ) % 4;a = r + dir[t][0];b = c + dir[t][1];if ( a < h && a >= 0 && b < w && b >= 0 && Maze[a][b] != '#' )return dfs ( a, b, (t+3) % 4, dir ) + 1;}return -1;}int bfs(){memset(vis,0,sizeof(vis));queue<Point> Q;Point current, next;current.step = 1;current.r = sr;current.c = sc;vis[sr][sc] = true;Q.push ( current );while ( ! Q.empty () ){current = Q.front ();Q.pop ();if ( current.r == er && current.c == ec )return current.step;for ( int i = 0; i < 4; i++ ){int a = current.r + dirL[i][0];int b = current.c + dirL[i][1];if ( a < h && a >= 0 && b < w && b >= 0  && Maze[a][b] != '#' && ! vis[a][b] ){next.r = a;next.c = b;vis[a][b] = true;next.step = current.step + 1;Q.push ( next );}}}return -1;}int main(){int n, way;scanf("%d",&n); while ( n-- ){scanf("%d%d",&w,&h); getchar();for ( int i = 0; i < h; i++ ){scanf("%s", Maze[i]);for ( int j = 0; j < w; j++ ){if ( Maze[i][j] == 'S' ){sr = i;sc = j;}if ( Maze[i][j] == 'E' ){er = i;ec = j;}}}if ( sr == 0 )way = 0;else if ( sc == 0 )way = 1;else if ( sr == h - 1 )way = 2;else if ( sc == w - 1 )way = 3;printf("%d %d %d\n", dfs(sr,sc,way,dirL), dfs(sr,sc,way,dirR), bfs() );}return 0;}