3 条题解

  • 0
    @ 2024-12-3 12:42:48

    不判空居然没问题 判空AC:

    #include <bits/stdc++.h>
    #define LL long long
    using namespace std;
    const int N = 30 + 10;
    const int INF = 0x3f3f3f3f;
    struct node
    {
    	int x , y , z , time;
    };
    queue < node > q;
    bool flag;
    int dx [] = {1 , -1 , 0 , 0 , 0 , 0};
    int dy [] = {0 , 0 , 1 , -1 , 0 , 0};
    int dz [] = {0 , 0 , 0 , 0 , 1 , -1};
    int l , n , m , sx , sy , sz , ex , ey , ez; 
    char a [N] [N] [N];
    bool check (int x , int y , int z)
    {
    	if (x >= 1 && x <= l && y >= 1 && y <= n && z >= 1 && z <= m && a [x][y][z] != '#')
    	    return 1;
    	return 0;
    }
    void bfs (int x , int y , int z)
    {
    	a [x] [y] [z] = '#';
    	q.push((node) {x , y , z , 0});
    	while ( !q.empty() )
    	{
    		node top = q.front();
    		q.pop(); 
    		if (top.x == ex && top.y == ey && top.z == ez)
    		{
    			printf ("%s%d%s\n" , "Escaped in " , top.time , " minute(s).");
    			flag = 1;
    			return;
    		}
    		for (int i = 0; i <= 5; i++)
    		{
    			int xx = top.x + dx [i]; 
    			int yy = top.y + dy [i]; 
    			int zz = top.z + dz [i];
    			if (check (xx , yy , zz)) 
    			{
    				q.push((node) {xx , yy , zz , top.time + 1});
    				a [xx] [yy] [zz] = '#';
    			}
    		}
    	}
    }
    int main()
    {
    	    while (cin >> l >> n >> m)
    	    {
    	    	if (l == 0 && n == 0 && m == 0) return 0;
    	    	for (int i = 1; i <= l; i++)
    	    	{
    	    		for (int j = 1; j <= n; j++)
    	    		{
    	    			for (int k = 1; k <= m; k++) 
    	                {
    	            	    cin >> a [i] [j] [k];
    	            	    if (a [i] [j] [k] == 'S') sx = i , sy = j , sz = k;
    	            	    if (a [i] [j] [k] == 'E') ex = i , ey = j , ez = k;
    				    }
    				}      
    			} 
    	        bfs (sx , sy , sz);     
    			if (!flag)   
    	            printf ("%s\n" , "Trapped!");
    	        flag = 0;
    	        while (!q.empty())  
    	            q.pop();
    		} 
    		return 0;
    }
    

    信息

    ID
    1828
    时间
    1000ms
    内存
    256MiB
    难度
    7
    标签
    递交数
    96
    已通过
    24
    上传者