首页 > 代码库 > poj1979 解题报告

poj1979 解题报告

//
//  main.cpp
//  poj1979
//
//  Created by 韩雪滢 on 11/28/16.
//  Copyright ? 2016 韩雪滢. All rights reserved.
//

#include <iostream>
using namespace std;

class Tile{
public:
    bool visited;
    int color;
};

Tile **room;
int sum;
int w,h;

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

void dfs(int x,int y){
    
    room[x][y].visited = true;

    for(int i=0;i<4;i++){
        int cx = x + changeX[i];
        int cy = y + changeY[i];
        
        if((0 <= cx) &&( cx < w) && (0 <= cy)&& ( cy< h) && !room[cx][cy].visited && (room[cx][cy].color == 0)){
            
            sum++;
            dfs(cx,cy);
        }
    }
}

int main() {
    int cx=0,cy=0;
    
    while(cin >> h >> w){
        if(w>0 && h>0)
        {
            sum = 1;
           room = new Tile*[w];
            for(int k=0;k<w;k++){
                room[k] = new Tile[h];
                for(int m=0;m<h;m++){
                    room[k][m].color = -2;
                    room[k][m].visited = false;
                }
            }
            
            for(int i=0;i<w;i++)
            {
                for(int j=0;j<h;j++)
                {
                    char tile;
                    cin >> tile;
                    if(tile == .)
                        room[i][j].color = 0;//  black tile
                    if(tile == #)
                        room[i][j].color = 1;//  white tile
                    if(tile == @)
                    {
                        room[i][j].color = -1;//  people
                        cx = i;
                        cy = j;
                    }
                }
            }
            
            dfs(cx,cy);
            cout << sum << endl;
           
        }
        else
            break;
    }
    
    
    return 0;
}

 

poj1979 解题报告