雑記

ものを書く練習。学びをまとめる場。

paizaスキルチェックをやってみた

おはようございます。今日はコーディングの日です。

先日教えてもらった「paizaスキルチェック」というものをやってみました。

できそうで出来ないという。。。

ちなみに自分はまともに書けるのがC言語くらいなのでCで挑戦しています。

A009:ビームの反射

下みたいなコードを書いたのですが80点しかもらえませんでした。なんでだ。。。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define OUT 0
#define IN 1
#define RIGHT 2
#define UP 3
#define LEFT 4
#define DOWN 5

int main(void)
{
    // 自分の得意な言語で
    // Let's チャレンジ!!

    char str[1000];
    char *tp;
    int H, W;
    fgets(str, sizeof(str), stdin);
    tp = strtok(str, " ");
    H = (int) strtol(tp, NULL, 10);
    tp = strtok(NULL, " ");
    W = (int) strtol(tp, NULL, 10);
    // printf("%d %d\n", H, W);
    
    int i, j;
    char field[H][W];
    for ( i = 0; i < H; i++ ){
        fgets(str, sizeof(str), stdin);
        for ( j = 0; j < W; j++ )
            field[i][j] = str[j];
    }
    
    int count = 1;
    int flag = IN;
    int vector = RIGHT;
    int h = 0; int w = 0; // current position
    while ( flag ){
        switch ( vector ){
            case RIGHT:
                if ( w+1 == W )
                    flag = OUT;
                else if ( field[h][w+1] == '_' ){
                    w++;
                    count++;
                }
                else if ( field[h][w+1] == '/' ){
                    w++;
                    vector = UP;
                    count++;
                }
                else if ( field[h][w+1] == '\\' ){
                    w++;
                    vector = DOWN;
                    count++;
                    // printf("CP:%d %d", h, w);
                }
                break;
            case LEFT:
                if ( w-1 < 0 )
                    flag = OUT;
                else if ( field[h][w-1] == '_' ){
                    w--;
                    count++;
                }
                else if ( field[h][w-1] == '/' ){
                    w--;
                    vector = DOWN;
                    count++;
                }
                else if ( field[h][w-1] == '\\' ){
                    w--;
                    vector = UP;
                    count++;
                }
                break;
            case UP:
                if ( h-1 < 0 )
                    flag = OUT;
                else if ( field[h-1][w] == '_' ){
                    h--;
                    count++;
                }
                else if ( field[h-1][w] == '/' ){
                    h--;
                    vector = RIGHT;
                    count++;
                }
                else if ( field[h-1][w] == '\\' ){
                    h--;
                    vector = LEFT;
                    count++;
                }
                break;
            case DOWN:
                if ( h+1 == H )
                    flag = OUT;
                else if ( field[h+1][w] == '_' ){
                    h++;
                    count++;
                }
                else if ( field[h+1][w] == '/' ){
                    h++;
                    vector = LEFT;
                    count++;
                }
                else if ( field[h+1][w] == '\\' ){
                    h++;
                    vector = RIGHT;
                    count++;
                }
                break;
            default:
                break;
        }
    }
    
    printf("%d\n", count);
    
    return 0;
}

わかったらまたupしようと思います。