zoukankan      html  css  js  c++  java
  • 【例题 7-9 UVA-1601】The Morning after Halloween

    【链接】 我是链接,点我呀:)
    【题意】

    在这里输入题意

    【题解】

    对于没有出现的,当成0节点就好。 所以总是认为有3个人需要走到各自的终点。 将平面图转成点边图。这样比较好枚举。 (二维变成一维,模拟的时候变量都少了一半啦) 然后每次按照要求模拟走一下就好。 (三重循环,枚举每一个人下一步走到了哪个位置即可 (注意两个0节点可以走到相同的位置->因为实际上他们都不存在); 然后用一个三元组加入队列里。 f[x][y][z]来判重就好了。 (直接用map来判重会TLE到死。。

    【代码】

    /*
      	1.Shoud it use long long ?
      	2.Have you ever test several sample(at least therr) yourself?
      	3.Can you promise that the solution is right? At least,the main ideal
      	4.use the puts("") or putchar() or printf and such things?
      	5.init the used array or any value?
      	6.use error MAX_VALUE?
      	7.use scanf instead of cin/cout?
      	8.whatch out the detail input require
    */
    #include <bits/stdc++.h>
    using namespace std;
    
    typedef pair<int,pair<int,int> > piii;
    
    const int N = 256;
    const int NN = 16;
    const int dx[4] = {0,0,1,-1};
    const int dy[4] = {1,-1,0,0};
    
    int n,m,tot;
    int v[3][2];
    char s[NN+5][NN+5];
    vector <int> g[N+10];
    int dic[N+10][N+10][N+10];
    queue <piii> dl;
    
    bool jue(int x,int y){
     	if (x==0 || y==0) return false;
     	if (x==y) return true;
     	return false;
    }
    
    int main(){
    	#ifdef LOCAL_DEFINE
    	    freopen("F:\c++source\rush_in.txt", "r", stdin);
    	    freopen("F:\c++source\rush_out.txt", "w", stdout);
    
    	#endif
    	ios::sync_with_stdio(0),cin.tie(0);
    	while (cin >> m>> n >> tot){
    		if (n==0 && m==0 && tot==0) break;
    		cin.get();
    		memset(v,0,sizeof v);
    		for (int i = 0;i <=N;i++) g[i].clear();
    		for (int i = 0;i <= N;i++) g[i].push_back(i);
    
    		for (int i = 1;i <= n;i++) cin.getline(s[i]+1,N+10);
    
    		for (int i = 1;i <= n;i++)
    			for (int j = 1;j <= m;j++)
        			if (s[i][j]!='#'){
        				for (int k = 0;k < 4;k++){
        				 	int ti = i + dx[k],tj = j + dy[k];
        					if (ti >=1 && ti <= n && tj>=1 && tj<=m && s[ti][tj]!='#'){
        						int x = (i-1)*m+j,y = (ti-1)*m+tj;
        						g[x].push_back(y);					 	
        					}
        				}
        				for (int k = 0;k < tot;k++){
        					if (s[i][j]==('a'+k)) v[k][0] = (i-1)*m+j;	
        					if (s[i][j]==('A'+k)) v[k][1] = (i-1)*m+j;
        				}
        			}
    
        	memset(dic,-1,sizeof dic);
    
    		dic[v[0][0]][v[1][0]][v[2][0]] = 0;
    		piii temp = make_pair(v[0][0],make_pair(v[1][0],v[2][0]));
    		dl.push(temp);
    		while (!dl.empty()){
    		 	temp = dl.front();
    		 	dl.pop();
    		 	int x = temp.first,y = temp.second.first,z = temp.second.second;
    		 	for (int i = 0;i < (int)g[x].size();i++)
    		 		for (int j = 0;j < (int)g[y].size();j++)
    		 			for (int k = 0;k < (int)g[z].size();k++){
    		 				int tx = g[x][i],ty = g[y][j],tz = g[z][k];
    		 				if (jue(tx,ty) || jue(tx,tz) || jue(ty,tz)) continue;
    		 				if (!(x==0||y==0)&&x == ty && tx == y) continue;
    		 				if (!(x==0||z==0)&&x == tz && tx == z) continue;
    		 				if (!(y==0||z==0)&&y == tz && ty == z) continue;
    
    		 				piii temp1 = make_pair(tx,make_pair(ty,tz));
    		 				if (dic[tx][ty][tz]==-1){
    		 				 	dic[tx][ty][tz] = dic[x][y][z] + 1;
    		 				 	dl.push(temp1);
    		 				}
    			 		}
    		}
    		cout << dic[v[0][1]][v[1][1]][v[2][1]] <<endl;
    	}  	
    	return 0;
    }
    
  • 相关阅读:
    死循环(endless loop)
    while循环和do-while循环语句
    99乘法表(for循环嵌套)
    代码块和作用域
    Break语句和Continue语句
    习题两则的简化(利用for循环)
    for循环语句
    条件判断语句(if-else)
    习题两则(自增操作符和数据类型相关知识点)
    字符集、编码和字符串
  • 原文地址:https://www.cnblogs.com/AWCXV/p/8021418.html
Copyright © 2011-2022 走看看