2016年5月22日 星期日

[UVA] 11995 - I Can Guess the Data Structure!

/* 題目: UVa 11995 - I Can Guess the Data Structure!
 * Language: C++
 * Created on: 2016年5月22日
 *   Author: hanting
 */
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
int main()
{
    int N;
    while(cin >> N)
    {
        bool test[3] = {};
        queue<int> que;
        priority_queue<int> pque;
        stack<int> stk;
        int op;
        int num;
        for(int i = 0; i < N; i++)
        {
            cin >> op >> num;
            if(op == 1)
            {
                que.push(num);
                pque.push(num);
                stk.push(num);
            }
            else //if(op == 2)
            {
                int tmp;
                if(test[0] == 0)
                {
                    if(que.size())
                    {
                        tmp = que.front();
                        if(tmp != num)
                        {
                            test[0] = 1;
                        }
                        else
                        {
                            que.pop();
                        }
                    }
                    else
                    {
                        test[0] = 1;
                    }
                }
                if(test[1] == 0)
                {
                    if(pque.size())
                    {
                        tmp = pque.top();
                        if(tmp != num)
                        {
                            test[1] = 1;
                        }
                        else
                        {
                            pque.pop();
                        }
                    }
                    else
                    {
                        test[1] = 1;
                    }
                }
                if(test[2] == 0)
                {
                    if(stk.size())
                    {
                        tmp = stk.top();
                        if(tmp != num)
                        {
                            test[2] = 1;
                        }
                        else
                        {
                            stk.pop();
                        }
                    }
                    else
                    {
                        test[2] = 1;
                    }
                }
            }
        }
        int cnt = 0;
        for(int i = 0; i < 3; i++)
        {
            if(test[i] == 0) cnt++;
        }
        if(cnt == 0)
        {
            cout << "impossible" << endl;
        }
        else if(cnt == 1)
        {
            if(test[0] == 0)
            {
                cout << "queue" << endl;
            }
            else if(test[1] == 0)
            {
                cout << "priority queue" << endl;
            }
            else
            {
                cout << "stack" << endl;
            }
        }
        else
        {
            cout << "not sure" << endl;
        }
    }

    return 0;
}

2016年5月7日 星期六

[UVA] 11195 - Another n-Queen Problem

題目連結



==================================================
程式碼:
/* 題目: UVa 11195 - Another n-Queen Problem
 * Language: C++
 * Created on: 2016年05月08日
 *   Author: hanting
 */
#include <iostream>
#include <vector>
#include <string>
#include <bitset>
using namespace std;
#pragma warning(disable:4996)
int ans;
vector<int > arr;
vector<string> vec;
void DFS(int row, int ld, int rd, int N, int cnt)
{
if (cnt == N)
{
ans++;
return;
}
int next = row | ld | rd;
for (int i = 0; i < N; i++)
{
int mask = 1 << i;
if (!(next & mask) && vec[cnt][i] != '*')
{
DFS(row|mask, (ld|mask) << 1, (rd|mask) >> 1, N, cnt + 1);
}
}
}
int main()
{
int N;
int CaseN = 1;
while (cin >> N && N)
{
ans = 0;
arr.assign(N, 0);
vec.assign(N, "");
for (int i = 0; i < N; i++)
{
cin >> vec[i];
}
DFS(arr[0], 0, 0, N, 0);
cout << "Case " << CaseN++ << ": " << ans << endl;
}
}

2016年5月1日 星期日

[UVA] 11956 - Brainfuck

/* 題目: UVa 11956 - Brainfuck
 * Language: C++
 * Created on: 2016年05月01日
 *   Author: hanting
 */
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    int CaseN = 1;
    int testCase = 0;
    cin >> testCase;
    cin.get();
    while(testCase--)
    {
        cout << dec << "Case " << CaseN++ << ":";
        string ins;
        getline(cin, ins);
        unsigned char memory[100] = {};
        int ptr = 0;
        for(int i = 0; i < ins.size(); i++)
        {
            if(ins[i] == '+') memory[ptr]++;
            else if(ins[i] == '-') memory[ptr]--;
            else if(ins[i] == '>') ptr++;
            else if(ins[i] == '<') ptr--;
            if(ptr < 0) ptr = 99;
            else if(ptr > 99) ptr = 0;
        }
        for(int i = 0; i < 100; i++)
        {
            cout << " ";
            cout << setw(2);
            cout << uppercase << setfill('0') << hex << (int)memory[i];
        }
        cout << endl;
    }
    return 0;
}