Showing posts with label HDU. Show all posts
Showing posts with label HDU. Show all posts

Thursday, July 14, 2016

【HDU】Red and Black

Problem here

Problem Description

There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.Write a program to count the number of black tiles which he can reach by repeating the moves described above.

Input

The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.'.' - a black tile '#' - a red tile '@' - a man on a black tile(appears exactly once in a data set)

Output

For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).

Sample Input

6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
Sample Output
45

Source

Solution

#include <iostream>
#include <memory.h>
using namespace std;
int w, h;
int map[100][100];
int cnt;

void dfs(int x, int y){
    if(x < 0 || y < 0 || x >= h || y >= w || map[x][y] == 1)
        return;

    cnt++;
    map[x][y] = 1;
    dfs(x+1, y);
    dfs(x, y+1);
    dfs(x-1, y);
    dfs(x, y-1);
}

int main(){
    while(cin >> w >> h){
        if(w == 0 && h == 0)
            break;

        int p, q;
        cnt = 0;
        memset(map, 0, sizeof(map));
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                char input;
                cin >> input;
                if(input == '@'){
                    p = i; 
                    q = j;
                }
                if(input == '#')
                    map[i][j] = 1;

            }
        }

        dfs(p, q);
        cout << cnt << endl;
    }

    return 0;
}

Friday, April 15, 2016

【HDU】2041-超级楼梯

Problem here

Problem

有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法?

INPUT

输入数据首先包含一个整数N,表示测试实例的个数,然后是N行数据,每行包含一个整数M(1<=M<=40),表示楼梯的级数。

OUTPUT

对于每个测试实例,请输出不同走法的数量

Sample

input



3

output


2

Solution

設x為樓梯數 
f(x)為上樓梯的方法 
因為已知 
f(1)=1 <—因為一開始就站在第一級,所以不用再走上一級 
f(2)=1<—只有兩級樓梯,所以只能向上走一級 
f(3)=2<—每次只能走一或兩級,所以只有兩種方法 
f(4)=f(3)+f(2)=3<—當只走上一級時,就會變成f(3)的情況;當只走上兩級時,就會變成f(2)的情況 



etc. 
由此得知 
f(x) = f(x-1)+f(x-2)
#include <iostream>
using namespace std;

int main(){

    int n;
    while(cin >> n){
        int s[50];
        s[1] = 1;
        s[2] = 1;
        for(int i = 3; i <= 40; i++){
            s[i] = s[i-1] + s[i-2];
        }
        while(n--){
            int m;
            cin >> m;
            cout << s[m] << endl;
        }
    }

    return 0;
}

Installing xAct (Mac)

Download from http : www.xact.es . Before unpack the tar/tgz file, remove attributes with xattr -c xAct_1.x.x.tgz then place the un...