2014年4月19日 星期六

Sudoku Solver



What is this?

This is a program which can solve sudoku for those people who are tired of solving difficult sudoku problem.

How to use?

1. Input the sudoku problem into the board.
2. Click "Solve It!" to solve sudoku, and you will see the answer on the board.
3. If you want to input another sudoku problem, just click "Clear" to clean the board and input again.

118 - Mutant Flatworld Explorers

Background

Robotics, robot motion planning, and machine learning are areas that cross the boundaries of many of the subdisciplines that comprise Computer Science: artificial intelligence, algorithms and complexity, electrical and mechanical engineering to name a few. In addition, robots as ``turtles'' (inspired by work by Papert, Abelson, and diSessa) and as ``beeper-pickers'' (inspired by work by Pattis) have been studied and used by students as an introduction to programming for many years.
This problem involves determining the position of a robot exploring a pre-Columbian flat world.

The Problem

Given the dimensions of a rectangular grid and a sequence of robot positions and instructions, you are to write a program that determines for each sequence of robot positions and instructions the final position of the robot.
A robot position consists of a grid coordinate (a pair of integers: x-coordinate followed by y-coordinate) and an orientation (N,S,E,W for north, south, east, and west). A robot instruction is a string of the letters 'L', 'R', and 'F' which represent, respectively, the instructions:
  • Left: the robot turns left 90 degrees and remains on the current grid point.
  • Right: the robot turns right 90 degrees and remains on the current grid point.
  • Forward: the robot moves forward one grid point in the direction of the current orientation and mantains the same orientation.
The direction North corresponds to the direction from grid point (x,y) to grid point (x,y+1).
Since the grid is rectangular and bounded, a robot that moves ``off'' an edge of the grid is lost forever. However, lost robots leave a robot ``scent'' that prohibits future robots from dropping off the world at the same grid point. The scent is left at the last grid position the robot occupied before disappearing over the edge. An instruction to move ``off'' the world from a grid point from which a robot has been previously lost is simply ignored by the current robot.

The Input

The first line of input is the upper-right coordinates of the rectangular world, the lower-left coordinates are assumed to be 0,0.
The remaining input consists of a sequence of robot positions and instructions (two lines per robot). A position consists of two integers specifying the initial coordinates of the robot and an orientation (N,S,E,W), all separated by white space on one line. A robot instruction is a string of the letters 'L', 'R', and 'F' on one line.
Each robot is processed sequentially, i.e., finishes executing the robot instructions before the next robot begins execution.
Input is terminated by end-of-file.
You may assume that all initial robot positions are within the bounds of the specified grid. The maximum value for any coordinate is 50. All instruction strings will be less than 100 characters in length.

The Output

For each robot position/instruction in the input, the output should indicate the final grid position and orientation of the robot. If a robot falls off the edge of the grid the word ``LOST'' should be printed after the position and orientation.

Sample Input

5 3
1 1 E
RFRFRFRF
3 2 N
FRRFLLFFRRFLL
0 3 W
LLFFFLFLFL

Sample Output

1 1 E
3 3 N LOST
2 3 S
 
出處: UVa Online Judge - Mutant Flatworld Explorers


問題敘述

此問題會先輸入兩個整數,表示這個矩形右上角的座標,接著會輸入多組測試資料,每組測試資料兩行,一直到輸入結束為止。每組測試資料第一行輸入的是兩個整數及一個字元,兩個整數代表機器人所在的初始的座標,字元表示面對的方位(E=東, W=西, S=南, N=北),第二行輸入的則是一行字串代表著一連串的指令(L=左轉, R=右轉, F=往現在面對的方位走一格)。每組輸入會對應一組輸出,此問題要求您輸出經過一連串的指令後,機器人最後所在的座標以及面對的方位為何。若機器人在執行指令的過程中跑出了此矩形的範圍,就輸出它在跑出去前的座標和方位並且印出 LOST,若機器人下次執行指令時,再次在之前跑出去過的座標往矩形外面跑,則此指令視為無效,將會被忽略。

解題思路

這是一個模擬類型的題目,沒有什麼特別的思路,但是要善用資料結構。比如說要如何知道左轉或右轉會轉到哪個方位呢? 我的作法是用一個陣列存{'E', 'N', 'W', 'S'},然後用一個 index 表示現在所在的位置,index + 1 就是左轉,index - 1 就是右轉,但是注意 index 做加減的時候可能會超出陣列範圍,所以要做環狀處理(意即 index 等於陣列的長度的時候要讓 index = 0,index 小於 0 的時候要讓 index = 陣列的長度 - 1)。其他的就把程式碼交給讀者們參考了。

c++ 程式碼

#include <iostream>
#include <map>
#include <vector>
using namespace std;

bool checkIsLost(int x, int y, int maxX, int maxY) {
    return x < 0 || x > maxX || y < 0 || y > maxY;
}

int main() {
    int maxX, maxY, x, y;
    char ori;
    string ins;
    char oriArr[4] = {'E', 'N', 'W', 'S'};
    int oriArrLen = 4;
    map<char, int> oriIndexMap;

    oriIndexMap['E'] = 0;
    oriIndexMap['N'] = 1;
    oriIndexMap['W'] = 2;
    oriIndexMap['S'] = 3;
    cin >> maxX >> maxY;
    vector< vector<bool> > lostTable(maxX + 1, vector<bool>(maxY + 1, false));
    while (cin >> x >> y >> ori >> ins) {
        int oriIndex;
        bool lost = false;

        oriIndex = oriIndexMap[ori];
        for (int i=0; i<ins.length(); i++) {
            switch (ins[i]) {
                case 'L':
                    oriIndex = (oriIndex + 1) % oriArrLen;
                    break;
                case 'R':
                    if (oriIndex)
                        oriIndex--;
                    else
                        oriIndex = oriArrLen - 1;
                    break;
                case 'F':
                    switch (oriIndex) {
                        case 0:
                            lost = checkIsLost(x + 1, y, maxX, maxY);
                            if (!lost)
                                x++;
                            break;
                        case 1:
                            lost = checkIsLost(x, y + 1, maxX, maxY);
                            if (!lost)
                                y++;
                            break;
                        case 2:
                            lost = checkIsLost(x - 1, y, maxX, maxY);
                            if (!lost)
                                x--;
                            break;
                        case 3:
                            lost = checkIsLost(x, y - 1, maxX, maxY);
                            if (!lost)
                                y--;
                    }
            }
            if (lost && !lostTable[x][y]) {
                lostTable[x][y] = true;
                break;
            }
        }
        cout << x << " " << y << " " << oriArr[oriIndex];
        if (lost)
            cout << " " << "LOST\n";
        else
            cout << endl;
    }

    return 0;
}

2014年4月7日 星期一

102 - Ecological Bin Packing

Background

Bin packing, or the placement of objects of certain weights into different bins subject to certain constraints, is an historically interesting problem. Some bin packing problems are NP-complete but are amenable to dynamic programming solutions or to approximately optimal heuristic solutions.
In this problem you will be solving a bin packing problem that deals with recycling glass.

The Problem

Recycling glass requires that the glass be separated by color into one of three categories: brown glass, green glass, and clear glass. In this problem you will be given three recycling bins, each containing a specified number of brown, green and clear bottles. In order to be recycled, the bottles will need to be moved so that each bin contains bottles of only one color.
The problem is to minimize the number of bottles that are moved. You may assume that the only problem is to minimize the number of movements between boxes.
For the purposes of this problem, each bin has infinite capacity and the only constraint is moving the bottles so that each bin contains bottles of a single color. The total number of bottles will never exceed 2^31.

The Input

The input consists of a series of lines with each line containing 9 integers. The first three integers on a line represent the number of brown, green, and clear bottles (respectively) in bin number 1, the second three represent the number of brown, green and clear bottles (respectively) in bin number 2, and the last three integers represent the number of brown, green, and clear bottles (respectively) in bin number 3. For example, the line 10 15 20 30 12 8 15 8 31
indicates that there are 20 clear bottles in bin 1, 12 green bottles in bin 2, and 15 brown bottles in bin 3.
Integers on a line will be separated by one or more spaces. Your program should process all lines in the input file.

The Output

For each line of input there will be one line of output indicating what color bottles go in what bin to minimize the number of bottle movements. You should also print the minimum number of bottle movements.
The output should consist of a string of the three upper case characters 'G', 'B', 'C' (representing the colors green, brown, and clear) representing the color associated with each bin.
The first character of the string represents the color associated with the first bin, the second character of the string represents the color associated with the second bin, and the third character represents the color associated with the third bin.
The integer indicating the minimum number of bottle movements should follow the string.
If more than one order of brown, green, and clear bins yields the minimum number of movements then the alphabetically first string representing a minimal configuration should be printed.

Sample Input

1 2 3 4 5 6 7 8 9
5 10 5 20 10 5 10 20 10

Sample Output

BCG 30
CBG 50

出處: UVa Online Judge - Ecological Bin Packing


問題敘述

此問題會有很多行輸入,每一行都會有九個整數,前三個整數分別代表在第一個回收桶裡棕色瓶子, 綠色瓶子和透明瓶子的數量,中間三個整數分別代表在第二個回收桶裡棕色瓶子, 綠色瓶子和透明瓶子的數量,最後三個整數分別代表在第三個回收桶裡棕色瓶子, 綠色瓶子和透明瓶子的數量。此問題要求您用最少的次數移動瓶子,來讓每個桶子都只裝一種顏色,並且輸出每個桶子各擺哪些顏色以及最少移動的次數。若不只一種移動方法可以讓每個桶子都只裝一種顏色且移動次數最少,則字母排列順序比較優先的要被輸出。

解題思路

因為瓶子的顏色只有三種,總共也只有六種排列方式,故暴力法是最簡單直接的解法。照字母順序把六種擺放瓶子顏色的方式都算出移動次數,並照順序找出移動次數最小的排列方式就是解答。

c++ 程式碼

#include <iostream>
#include <vector>
#include <map>
using namespace std;

int main() {
    int start;

    while (cin >> start) {
        vector< vector<int> > binBottle(3, vector<int>(3, -1));
        vector<string> comb(6, "");
        map<string, int> getCount;
        string minComb;

        comb[0] = "BCG";
        comb[1] = "BGC";
        comb[2] = "CBG";
        comb[3] = "CGB";
        comb[4] = "GBC";
        comb[5] = "GCB";
        for (int i=0; i<binBottle.size(); i++) {
            for (int j=0; j<binBottle[i].size(); j++) {
                if (!i && !j)
                    binBottle[i][j] = start;
                else
                    cin >> binBottle[i][j];
            }
        }
        getCount[comb[0]] = binBottle[1][0] + binBottle[2][0] + binBottle[0][2] + binBottle[2][2] +
                            binBottle[0][1] + binBottle[1][1];
        getCount[comb[1]] = binBottle[1][0] + binBottle[2][0] + binBottle[0][1] + binBottle[2][1] +
                            binBottle[0][2] + binBottle[1][2];
        getCount[comb[2]] = binBottle[1][2] + binBottle[2][2] + binBottle[0][0] + binBottle[2][0] +
                            binBottle[0][1] + binBottle[1][1];
        getCount[comb[3]] = binBottle[1][2] + binBottle[2][2] + binBottle[0][1] + binBottle[2][1] +
                            binBottle[0][0] + binBottle[1][0];
        getCount[comb[4]] = binBottle[1][1] + binBottle[2][1] + binBottle[0][0] + binBottle[2][0] +
                            binBottle[0][2] + binBottle[1][2];
        getCount[comb[5]] = binBottle[1][1] + binBottle[2][1] + binBottle[0][2] + binBottle[2][2] +
                            binBottle[0][0] + binBottle[1][0];
        minComb = comb[0];
        for (int i=1; i<comb.size(); i++) {
            if (getCount[minComb] > getCount[comb[i]])
                minComb = comb[i];
        }
        cout << minComb << " " << getCount[minComb] << endl;
    }

    return 0;
}