ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1008. Image Encoding

Why wrong?
Posted by Alireza Noori 4 Dec 2008 02:10
Hi, I wrote the solution but I don't know what it is wrong. Could you plz check the code for me? Cause I don't know if I get the point correctly.

#include <iostream>
#include <queue>

using namespace std;

char arr[10][10];

class Coord
{
public:
    Coord()
    {
    }

    Coord(int nx, int ny)
    {
        x = nx;
        y = ny;
    }
    int x, y;
};

void ReadFirst(int n, int x, int y)
{
    arr[x - 1][y - 1] = '1';

    for(int i = 1; i < n; i++)
    {
        cin >> x >> y;
        arr[x - 1][y - 1] = '1';
    }
}

Coord neighbor(int x, int y, char ch)
{
    switch(ch)
    {
    case 'R':return Coord(x + 1, y);
    case 'T':return Coord(x, y + 1);
    case 'L':return Coord(x - 1, y);
    case 'B':return Coord(x, y - 1);
    default: return Coord(x, y);
    }
}

void ReadSecond(int x, int y, char ch)
{
    queue<Coord> theQueue;
    Coord tCor;
    arr[x][y] = '1';

    while(ch != '.')
    {
        tCor = neighbor(x, y, ch);
        arr[tCor.x][tCor.y] = '1';
        theQueue.push(tCor);

        while(true)
        {
            ch = cin.get();

            while(iswspace(ch))
                ch = cin.get();

            if(ch == ',')
            {
                tCor = theQueue.front();
                x = tCor.x;
                y = tCor.y;
                theQueue.pop();
            }
            else
                break;
        }
    }
}

void PrintFirst()
{
    int i, j, count = 0;
    for(i = 0; i < 10; i++)
    {
        for(j = 0; j < 10; j++)
        {
            if(arr[i][j] == '1')
                count++;
        }
    }

    cout << count << endl;

    for(i = 0; i < 10; i++)
    {
        for(j = 0; j < 10; j++)
        {
            if(arr[i][j] == '1')
            {
                cout << i + 1 << ' ' << j + 1 << endl;
            }
        }
    }
}

void PrintSecond()
{
    int x, y;
    bool flag = false;
    for(x = 0; x < 10 && !flag; x++)
    {
        for(y = 0; y < 10; y++)
        {
            if(arr[x][y] == '1')
            {
                flag = true;
                break;
            }
        }
    }

    //first line coordinate
    x--;
    cout << x + 1 << ' ' << y + 1 << endl;

    queue<Coord> theQueue;
    Coord curCor, tCor;
    theQueue.push(Coord(x, y));

    while(!theQueue.empty())
    {
        curCor = theQueue.front();
        theQueue.pop();

        x = curCor.x;
        y = curCor.y;

        arr[x][y] = '0';

        if(arr[x + 1][y] == '1')
        {
            theQueue.push(Coord(x + 1, y));
            cout.put('R');
            arr[x + 1][y] = '0';
        }

        if(arr[x][y + 1] == '1')
        {
            theQueue.push(Coord(x + 1, y));
            cout.put('T');
            arr[x][y + 1] = '0';
        }

        if(arr[x - 1][y] == '1')
        {
            theQueue.push(Coord(x + 1, y));
            cout.put('L');
            arr[x - 1][y] = '0';
        }

        if(arr[x][y - 1] == '1')
        {
            theQueue.push(Coord(x + 1, y));
            cout.put('B');
            arr[x][y - 1] = '0';
        }

        if(theQueue.empty())
            cout << '.' << endl;
        else
            cout << ',' << endl;
    }
}

void main()
{
    char ch, buffer[3];
    int n, m;

    for(int i = 0; i < 10; i++)
    {
        for(int j = 0; j < 10; j++)
            arr[i][j] = '0';
    }

    cin >> n;

    cin >> m;

    cin.get();
    ch = cin.get();

    if(ch >= '0' && ch <= '9')
    {
        buffer[0] = ch;
        ch = cin.get();
        if(ch == ' ')
        {
            buffer[1] = '\0';
        }
        else
        {
            buffer[1] = ch;
            buffer[2] = '\0';
        }

        int y = atoi(buffer);

        ReadFirst(n, m, y);
        PrintSecond();
    }
    else
    {
        ReadSecond(n - 1, m - 1, ch);
        PrintFirst();
    }
}