CCF 201312-5I’m stuck!

来源:互联网 发布:oracle数据库sqlplus 编辑:程序博客网 时间:2024/05/16 14:05

//主要利用宽度优先算法。把S出发的能够到达的点都保存栈中。然后遍历所有栈中的点,调用bfs算法,看是否能到达终点。如果能计数

//sum++.  所有最后的点就是 能够到达的点减去sum   再减去1(因为T在栈中时,不计数sum)


#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
using namespace std;

typedef struct point{
    int x;
    int y;
}point;

vector<point> reach;
queue<point> q;   //BFS
stack<point> s; // 开始可以到达的点
int sum = 0;
int flag = false;//标志bfs运行的具体内容
 
#define N 51
int hang = 0,lie = 0;
int startx,starty,endx,endy;

char map[N][N];
int visit[N][N];

int gox [4] = {1,-1,0,0} ; //上下左右
int goy[4]={0,0,1,-1};


void shizhou(int x,int y){
        for(int i = 0;i<4;i++)    
        {
            int tempx = x+gox[i];
            int tempy = y+goy[i];
            if(tempx<1||tempy<1 || tempx>hang || tempy>lie || map[tempx][tempy]=='#'){
                continue;
            }else{
                if(visit[tempx][tempy]==0&&flag== false){
                    point p1;
                    visit[tempx][tempy]=1;    
                    p1.x = tempx;
                    p1.y = tempy;
                    q.push(p1);
                    s.push(p1);
                }else if(visit[tempx][tempy]==0&&flag== true){
                    point p1;
                    visit[tempx][tempy]=1;    
                    p1.x = tempx;
                    p1.y = tempy;
                    q.push(p1);
                    if(tempx==endx&&tempy==endy){
                        sum++;
                    }
                }
            }
        }
}

void shangxia(int x, int y){
    
        for(int i = 0;i<=1;i++)    
        {
            int tempx = x+gox[i];
            int tempy = y+goy[i];
            if(tempx<1||tempy< 1|| tempx>hang||tempy>lie||map[tempx][tempy]=='#'){
                continue;
            }else{
                if(visit[tempx][tempy]==0&&flag== false){
                    point p1;
                    visit[tempx][tempy]=1;    
                    p1.x = tempx;
                    p1.y = tempy;
                    q.push(p1);
                    s.push(p1);
                }else if(visit[tempx][tempy]==0&&flag== true){
                    point p1;
                    visit[tempx][tempy]=1;    
                    p1.x = tempx;
                    p1.y = tempy;
                    q.push(p1);
                    if(tempx==endx&&tempy==endy){
                        sum++;
                    }
                }
            }
        }
}

void zuoyou(int x, int y){
    
        for(int i = 2;i<=3;i++)    
        {
            int tempx = x+gox[i];
            int tempy = y+goy[i];
            if(tempx<1||tempy<1||tempx>hang||tempy>lie||map[tempx][tempy]=='#'){
                continue;
            }else{
                if(visit[tempx][tempy]==0&&flag== false){
                    point p1;
                    visit[tempx][tempy]=1;    
                    p1.x = tempx;
                    p1.y = tempy;
                    q.push(p1);
                    s.push(p1);
                }else if(visit[tempx][tempy]==0&&flag== true){
                    point p1;
                    visit[tempx][tempy]=1;    
                    p1.x = tempx;
                    p1.y = tempy;
                    q.push(p1);
                    if(tempx==endx&&tempy==endy){
                        sum++;
                    }
                }
            }
        }
}

void xia(int x,int y){
    for(int i = 0;i<=0;i++)    
        {
            int tempx = x+gox[i];
            int tempy = y+goy[i];
            if(tempx<1||tempy<1||tempx>hang||tempy>lie||map[tempx][tempy]=='#'){
                continue;
            }else{
                if(visit[tempx][tempy]==0&&flag== false){
                    point p1;
                    visit[tempx][tempy]=1;    
                    p1.x = tempx;
                    p1.y = tempy;
                    q.push(p1);
                    s.push(p1);
                }else if(visit[tempx][tempy]==0&&flag== true){
                    point p1;
                    visit[tempx][tempy]=1;    
                    p1.x = tempx;
                    p1.y = tempy;
                    q.push(p1);
                    if(tempx==endx&&tempy==endy){
                        sum++;
                    }
                }
            }
        }
}

void bfs() {
    while(!q.empty()){
        point p = q.front();
        q.pop();
        int x = p.x;
        int y = p.y;
        visit[x][y] = 1;
    
        switch(map[x][y]){
            case 'S': case '+' : case 'T' :{
                shizhou(x,y);
                break;
            }
            case '|':{
                shangxia(x,y);
                break;
            }
            case '-': {
                zuoyou(x,y);
                break;
            }
            case '.':{
                xia(x,y);
                break;
            }    
        }    
    }
}

void bfs(queue<point> q) {
    while(!q.empty()){
        point p = q.front();
        q.pop();
        int x = p.x;
        int y = p.y;
        visit[x][y] = 1;
    
        switch(map[x][y]){
            case 'S': case '+' : case 'T' :{
                shizhou(x,y);
                break;
            }
            case '|':{
                shangxia(x,y);
                break;
            }
            case '-': {
                zuoyou(x,y);
                break;
            }
            case '.':{
                xia(x,y);
                break;
            }    
        }    
    }
}
int main(){
    
    cin >>hang>>lie;
    
    for(int i = 1;i<=hang;i++)    
    {
        for(int j = 1;j<=lie;j++)    
        {
            cin >>map[i][j];
            
            if(map[i][j]=='S'){
                startx = i;
                starty = j;
            }else if(map[i][j]=='T'){
                endx = i;
                endy = j;
            }
        }
    }
    
//    cout << startx <<" "<<starty<<endl;
//    cout <<endx <<" " << endy<<endl;
    
    
    point p1;
    p1.x = startx;
    p1.y = starty;
    q.push(p1);
    
    flag = false;
    bfs();
    //可达
    //cout <<"可达:"<<endl;
    flag = true; //开启另一个bfs
    
    
    bool firstReach = false;
    int num = s.size();
    if(s.empty()){
        cout <<"I'm stuck!";
    }else{
        while(!s.empty())
        {
            point p = s.top();
            s.pop();
            if(endx == p.x && endy == p.y){
                firstReach = true;
            }
            memset(visit,0,sizeof(visit)); //清零
            //清理q
            while(!q.empty()) {
                q.pop();
            }
            
            q.push(p);
            bfs();
        }
    }
    if(firstReach){
        cout << num-sum-1;    //sum 就是可以从S到达的点,还可以到达T的 (-1是因为T本身不算)
    }else{
        
        cout <<"I'm stuck!";
    }
    
    
    
    return 0;
        
}
0 0