zoukankan      html  css  js  c++  java
  • topcoder SRM 628 DIV2 BishopMove

    题目比较简单。

    注意看测试用例2,给的提示

    Please note that this is the largest possible return value: whenever there is a solution, there is a solution that uses at most two moves.

    最多只有两步

    #include <vector>
    #include <string>
    #include <list>
    #include <map>
    #include <set>
    #include <deque>
    #include <stack>
    #include <bitset>
    #include <algorithm>
    #include <functional>
    #include <numeric>
    #include <utility>
    #include <sstream>
    #include <iostream>
    #include <iomanip>
    #include <cstdio>
    #include <cmath>
    #include <cstdlib>
    #include <ctime>
    #include <cstring>
    #include <climits>
    #include <queue>
    
    using namespace std;
    
    class BishopMove
    {
    public:
        typedef pair<int,int> Point;
        int howManyMoves(int r1, int c1, int r2, int c2)
        {
            queue<Point> que;
            que.push(Point(r1,c1));
            int step = 0;
            bool visit[8][8];
            memset(visit,false,sizeof(visit));
            int dx[]={1,-1,1,-1};
            int dy[]={1,1,-1,-1};
            while(!que.empty()){
                int cnt = que.size();
                while(cnt-->0){
                    Point tmp = que.front();que.pop();
                    visit[tmp.first][tmp.second] = true;
                    if(tmp.first == r2 && tmp.second == c2) return step;
                    for(int k = 1; k < 8 ; ++ k){
                        for(int i = 0 ; i  < 4; ++ i){
                            int x = tmp.first+dx[i]*k,y=tmp.second+dy[i]*k;
                            if(x>=0 && x < 8 && y>=0 && y<8 && !visit[x][y]) que.push(Point(x,y));
                        }
                    }
                }
                step++;
            }
            return -1;
        }
    
    
    };
    
    
    // Powered by FileEdit
    // Powered by TZTester 1.01 [25-Feb-2003]
    // Powered by CodeProcessor
    BFS解法
    #include <vector>
    #include <string>
    #include <list>
    #include <map>
    #include <set>
    #include <deque>
    #include <stack>
    #include <bitset>
    #include <algorithm>
    #include <functional>
    #include <numeric>
    #include <utility>
    #include <sstream>
    #include <iostream>
    #include <iomanip>
    #include <cstdio>
    #include <cmath>
    #include <cstdlib>
    #include <ctime>
    #include <cstring>
    #include <climits>
    #include <queue>
    
    using namespace std;
    
    class BishopMove
    {
    public:
        typedef pair<int,int> Point;
        bool visit[8][8];
        Point start,endp;
        int res;
        const int dx[4]={1,-1,1,-1};
        const int dy[4]={1,1,-1,1 };
        void dfs(Point pos,int step){
            if(pos.first == endp.first && pos.second == endp.second){
                res=min(res,step);
                return;
            }
            if(step > res) return;
            for(int k = 1 ; k < 8; ++ k){
                for(int i = 0 ; i < 4; ++ i){
                    int x = pos.first+k*dx[i], y =pos.second+k*dy[i];
                    if(x>=0 && x< 8 && y>=0 && y < 8 && !visit[x][y]){
                        visit[x][y] = true;
                        dfs(Point(x,y),step+1);
                        visit[x][y] = false;
                    }
                }
            }
        }
    
        int howManyMoves(int r1, int c1, int r2, int c2)
        {
            memset(visit,false,sizeof(visit));
            start.first = r1;start.second =c1;
            endp.first = r2; endp.second = c2;
            res = 1000;
            visit[r1][c1] = true;
            dfs(start,0);
            if(res == 1000) res=-1;
            return res;
        }
    
    // BEGIN CUT HERE
        public:
        void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); }
        private:
        template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '"' << *iter << "","; os << " }"; return os.str(); }
        void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "	Expected: "" << Expected << '"' << endl; cerr << "	Received: "" << Received << '"' << endl; } }
        void test_case_0() { int Arg0 = 4; int Arg1 = 6; int Arg2 = 7; int Arg3 = 3; int Arg4 = 1; verify_case(0, Arg4, howManyMoves(Arg0, Arg1, Arg2, Arg3)); }
        void test_case_1() { int Arg0 = 2; int Arg1 = 5; int Arg2 = 2; int Arg3 = 5; int Arg4 = 0; verify_case(1, Arg4, howManyMoves(Arg0, Arg1, Arg2, Arg3)); }
        void test_case_2() { int Arg0 = 1; int Arg1 = 3; int Arg2 = 5; int Arg3 = 5; int Arg4 = 2; verify_case(2, Arg4, howManyMoves(Arg0, Arg1, Arg2, Arg3)); }
        void test_case_3() { int Arg0 = 4; int Arg1 = 6; int Arg2 = 7; int Arg3 = 4; int Arg4 = -1; verify_case(3, Arg4, howManyMoves(Arg0, Arg1, Arg2, Arg3)); }
    
    // END CUT HERE
    
    };
    // BEGIN CUT HERE
    int main()
    {
        BishopMove ___test;
        ___test.run_test(-1);
    }
    // END CUT HERE
    DFS解法

    由于题目最多是两步,故结果只能是-1,0,1,2

    当两个位置的奇偶性不同时,结果是-1,

    当两个位置相等时是0

    当abs(c) 与abs(r)相等的视乎,结果是1

    其他结果是2

    #include <vector>
    #include <string>
    #include <list>
    #include <map>
    #include <set>
    #include <deque>
    #include <stack>
    #include <bitset>
    #include <algorithm>
    #include <functional>
    #include <numeric>
    #include <utility>
    #include <sstream>
    #include <iostream>
    #include <iomanip>
    #include <cstdio>
    #include <cmath>
    #include <cstdlib>
    #include <ctime>
    #include <cstring>
    #include <climits>
    #include <queue>
    
    using namespace std;
    
    class BishopMove
    {
    public:
        int howManyMoves(int r1, int c1, int r2, int c2)
        {
            if(((r1+c1)%2) ^ ((r2+c2)%2) ) return -1;
            if(r1==r2 && c1 == c2 ) return 0;
            if(abs(r2-r1) == abs(c2-c1)) return 1;
            return 2;
        }
    };
    
    
    // Powered by FileEdit
    // Powered by TZTester 1.01 [25-Feb-2003]
    // Powered by CodeProcessor
    View Code
  • 相关阅读:
    第一章:计算机网络参考模型
    阿里云ECS hadoop+spark+zookeeper+hive code-server 集群搭建
    IDEA SSM+MAVEN+JWT 图书管理系统
    IDEA SSM后端框架入门
    code-server Command ' ' not found
    code-server scala error: object apache is not a member of package org
    下面给出一个child-parent的表格,要求挖掘其中的父子辈关系,给出祖孙辈关系的表格。
    现在有多个输入文件,每个文件中的每行内容均为一个整数。要求读取所有文件中的整数,进行升序排序后,输出到一个新的文件中,输出的数据格式为每行两个整数,第一个整数为第二个整数的排序位次,第二个整数为原待排列的整数。
    对于两个输入文件,即文件A 和文件B ,请编写MapReduce程序,对两个文件进行合并排除其中重复的内容,得到一个新的输出文件C。
    现有以下关系型数据库中的表(见表4-20表4-21和表4-22),要求将具转换为适合Hbase存储的表并插入数据。
  • 原文地址:https://www.cnblogs.com/xiongqiangcs/p/3862618.html
Copyright © 2011-2022 走看看