POJ3669 Meteor Shower 广度优先搜索

来源:互联网 发布:flash软件的利弊 编辑:程序博客网 时间:2024/05/20 22:40
    这题就是个简单的广度优先搜索,注意的是,搜索过的地方要进行标记更新下,否则的话会超时。
#ifndef HEAD#include <stdio.h>#include <vector>#include <math.h>#include <string.h>#include <string>#include <iostream>#include <queue>#include <list>#include <algorithm>#include <stack>#include <map>using namespace std;#endif // !HEAD#ifndef QUADMEMSETinline void QuadMemSet(void* dst, int iSize, int value){iSize = iSize / 4;int* newDst = (int*)dst;#ifdef WIN32__asm{mov edi, dstmov ecx, iSizemov eax, valuerep stosd}#elsefor (int i = 0; i < iSize; i++){newDst[i] = value;}#endif}#endifint MS[305][305];char MSVISITed[301][301];bool g_bFind = false;int g_iMaxTime = 0;int MSSolve(){if (MS[0][0] > 1001)return 0;if (MS[0][0] == 0)return -1;queue<pair<int, int> > node;queue<int> timenode;node.push(pair<int, int>(0, 0));MSVISITed[0][0] = 1;timenode.push(0);int ANS = 100000000;while (node.size() != 0){pair<int, int> xy = node.front();node.pop();int t = timenode.front();timenode.pop();MSVISITed[xy.first][xy.second] = 1;if ( t + 1 < MS[xy.first + 1][xy.second] && t + 1 < ANS && MSVISITed[xy.first + 1][xy.second] == 0){if (MS[xy.first + 1][xy.second] > 1001){return t + 1;}MS[xy.first + 1][xy.second] = t + 1;node.push(pair<int, int>(xy.first + 1, xy.second));timenode.push(t + 1);}if (t + 1 < MS[xy.first][xy.second + 1] && t + 1 < ANS &&  MSVISITed[xy.first][xy.second + 1] == 0){if (MS[xy.first][xy.second + 1] > 1001){return t + 1;}MS[xy.first ][xy.second + 1] = t + 1;node.push(pair<int, int>(xy.first, xy.second + 1));timenode.push(t + 1);}if (xy.first > 0 && t + 1 < MS[xy.first - 1][xy.second] && t + 1 < ANS &&  MSVISITed[xy.first - 1][xy.second] == 0){if (MS[xy.first - 1][xy.second] > 1001){return t + 1;}MS[xy.first - 1][xy.second] = t + 1;node.push(pair<int, int>(xy.first - 1, xy.second));timenode.push(t + 1);}if (xy.second > 0 && t + 1 < MS[xy.first][xy.second - 1] && t + 1 < ANS &&  MSVISITed[xy.first][xy.second - 1] == 0){if (MS[xy.first][xy.second - 1] > 1001){return t + 1;}MS[xy.first][xy.second - 1] = t + 1;node.push(pair<int, int>(xy.first, xy.second - 1));timenode.push(t + 1);}}if (ANS >= 100000000){return -1;}return ANS;}int main(){#ifdef _DEBUGfreopen("d:\\in.txt", "r", stdin);#endifint n, m;while (scanf("%d\n", &n) != EOF){QuadMemSet(MS, sizeof(MS), 2000);QuadMemSet(map1, sizeof(map1), 500);memset(MSVISITed, 0, sizeof(MSVISITed));int x, y, t;for (int i = 0; i < n; i++){scanf("%d %d %d\n", &x, &y, &t);MS[x][y] = min(MS[x][y], t);MS[x + 1][y] = min(MS[x + 1][y], t);MS[x][y + 1] = min(MS[x][y + 1], t);if (x > 0)MS[x - 1][y] = min(MS[x - 1][y], t);if (y > 0)MS[x][y - 1] = min(MS[x][y - 1], t);}printf("%d\n", MSSolve());break;}return 0;}

0 0
原创粉丝点击