码迷,mamicode.com
首页 > 其他好文 > 详细

wap 机试(lsp) 问题 暴力法

时间:2014-10-28 18:03:40      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:blog   io   os   ar   for   sp   on   2014   art   

#include <iostream>
#include <queue>
#include <assert.h>
#include <string>
#include <algorithm>
#include <fstream>
using namespace std;

const int dx[] = { -1 , 0 , 0, 1};
const int dy[] = { 0 ,-1 ,1 , 0 }; //4 direction

struct Point{
	Point(){};
	Point(int x,int y):m_x(x),m_y(y){}
	int m_x;
	int m_y;
	bool operator == (const Point & rPoint ){
		return rPoint.m_x == m_x && rPoint.m_y == m_y;
	}
};

class Orienteering{
public: 
	int main();
	bool isValid(const Point & p){
		bool ans = p.m_x >=0 && p.m_x < cMap.size() && p.m_y >= 0 && p.m_y < cMap[0].size() && cMap[p.m_x][p.m_y] != '#' ;
		return ans;
	}
	bool findG(const Point &cPoint,const Point &ePoint){
		for(int i = 0 ; i < 4 ; i ++ ){
			Point p(cPoint.m_x + dx[i] , cPoint.m_y + dy[i]);
			if(isValid(p) && !bVisited[p.m_x][p.m_y] && p == ePoint){
				return true;  // 只考虑一个
			}
			if(isValid(p) && ! bVisited[p.m_x][p.m_y]){
				Qtemp.push(p);
				bVisited[p.m_x][p.m_y] = true;
			}
		}
		return false;
	}
	void ClearQueue(queue<Point> &Q){
		while(!Q.empty()){
			Q.pop();
		}
	}

	void ResetVisted(){
		for(int i = 0 ; i < bVisited.size() ; i ++)
			for(int j = 0 ; j < bVisited[0].size() ; j ++ )
				bVisited[i][j] = false;
	}


	int findShortest(const Point & sPoint,const Point &ePoint){
		ResetVisted();
		ClearQueue(Qin);
		ClearQueue(Qtemp);

		shortestCount = 0;

		Qin.push(sPoint);
		bVisited[sPoint.m_x][sPoint.m_y] = true;
		while (true){
			while(!Qin.empty()){
				Point cur = Qin.front();
				if(findG(cur,ePoint)){
					shortestCount += 1;
					return shortestCount;
				}
				Qin.pop();
			}
			if(Qtemp.empty())
				return -1;// no way to
			else{
				shortestCount += 1; 
				swap(Qtemp,Qin);
			}
		}
		return -1;
	}


	void permutation(vector<vector<int>> & perms,int *a,int n,int k)
	{
		if(k == n){
			vector<int> temp(a,a+n);
			perms.push_back(temp);
			return;
		}
		for(int i = k; i < n ; i ++ ){
			swap(a[i],a[k]);
			permutation(perms,a,n,k+1);
			swap(a[i],a[k]);
		}

	}


	void readFile(string & filePath,vector<string> &cMpas){
		ifstream in(filePath);
		if(!in.is_open())
			return;
		string line;
		getline(in,line);
		sscanf(line.c_str(),"%d %d",&m,&n);
		for(int i = 0 ; i < m ; i ++ ){

			getline(in,line);
			cMpas.emplace_back(line);
		}
	}

	int m,n;
	int shortestCount;
	vector<vector<bool>> bVisited;
	queue<Point> Qin;
	queue<Point> Qtemp;
	vector<string> cMap;
};

int Orienteering::main(){
	/*
	int mapBackup[M][N];
	memcpy(mapBackup,path,sizeof(path)/sizeof(int));*/
	string filePath = "example1.txt";
	readFile(filePath,cMap);
	bVisited.resize(m,vector<bool>(n,false));

	vector<Point> ats;
	Point pStart;
	Point pEnd;

	for(int i = 0 ; i < m ; i ++ ){
		for(int j = 0 ; j < n ; j ++ ){
			if(cMap[i][j] == '@')
				ats.push_back(Point(i,j));
			if(cMap[i][j] == 'S')
				pStart = Point(i,j);
			if(cMap[i][j] == 'G')
				pEnd = Point(i,j);
		}
	}
	int gNums = ats.size();
	int *arrayNums = new int[gNums]; 
	for(int i = 0 ; i < gNums ; ++ i)
		arrayNums[i] = i;
	vector<vector<int>> perms;
	permutation(perms,arrayNums,gNums,0);
	int minLen = 100000;
	for(int i = 0 ; i < perms.size() ; i ++ ){
		int totalLen = 0;
		Point endP = ats[perms[i][0]];
		int len = findShortest(pStart,endP);
		if(len ==-1)
			return -1;
		totalLen += len;
		for(int j = 0 ; j < perms[i].size() -1 ; j ++ )
		{
			Point startPoint = ats[perms[i][j]];
			Point endPoint = ats[perms[i][j + 1]];
			int len = findShortest(startPoint,endPoint);
			if(len == -1)
				return -1;
			totalLen += len;
		}
		len = findShortest(ats[perms[i][perms[i].size()-1]],pEnd);
		if(len == -1)
			return -1;
		totalLen += len;
		minLen = min(totalLen,minLen);
	}
	delete[] arrayNums;
	return minLen;
}




int main(){
	Orienteering o;
	int ans = o.main();
	system("pause");
	return 0;
}

wap 机试(lsp) 问题 暴力法

标签:blog   io   os   ar   for   sp   on   2014   art   

原文地址:http://blog.csdn.net/yiranlun3/article/details/40540853

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!