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 << "\tExpected: \"" << Expected << ‘\"‘ << endl; cerr << "\tReceived: \"" << 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

topcoder SRM 628 DIV2 BishopMove

时间: 2024-07-31 21:58:44

topcoder SRM 628 DIV2 BishopMove的相关文章

topcoder srm 628 div2 250 500

做了一道题,对了,但是还是掉分了. 第二道题也做了,但是没有交上,不知道对错. 后来交上以后发现少判断了一个条件,改过之后就对了. 第一道题爆搜的,有点麻烦了,其实几行代码就行. 250贴代码: 1 #include <iostream> 2 #include <cstring> 3 #include <queue> 4 #include <cmath> 5 #include <cstdio> 6 #include <algorithm&g

TopCoder SRM 628 DIV 2

250-point problem Problem Statement    Janusz is learning how to play chess. He is using the standard chessboard with 8 rows and 8 columns. Both the rows and the columns are numbered 0 through 7. Thus, we can describe each cell using its two coordina

topcoder SRM 618 DIV2 WritingWords

只需要对word遍历一遍即可 int write(string word) { int cnt = 0; for(int i = 0 ; i < word.length(); ++ i){ cnt+=word[i]-'A'+1; } return cnt; } topcoder SRM 618 DIV2 WritingWords,布布扣,bubuko.com

topcoder SRM 618 DIV2 MovingRooksDiv2

一开始Y1,Y2两个参数看不懂,再看一遍题目后才知道,vector<int>索引代表是行数,值代表的是列 此题数据量不大,直接深度搜索即可 注意这里深度搜索的访问标识不是以前的索引和元素,而是一个交换元素后的整个状态vector<int>,这样可以避免重复元素的搜索 set<vector<int> > visit; bool flag; void dfs(vector<int>& src, vector<int>& d

topcoder SRM 618 DIV2 LongWordsDiv2

此题给出的条件是: (1)word的每个字母都是大写字母(此条件可以忽略,题目给的输入都是大写字母) (2) 相等字符不能连续,即不能出现AABC的连续相同的情况 (3)word中不存在字母组成xyxy的形式,即不存在第一个字符和第3个字符相等同时第2个字符和第4个字符相等的情况 对于第(2)种情况,只需要考虑word[i]!=word[i-1]即可 对于第(3)种情况,用一个4重循环遍历每种可能的情况,然后第一个字符和第3个字符相等同时第2个字符和第4个字符相等,则输出“DisLikes”即可

TOPCODER SRM 686 div2 1000

// TOPCODER SRM 686 div2 1000 Problem Statement 给出一个至多长 100 的字符串,仅包含 ( 和 ),问其中有多少个不重复的,合法的括号子序列. 子序列可以不连续:合法即括号序列的合法:答案模 1,000,000,007. Examples "(())(" Returns: 2 Correct non-empty bracket subsequences are "()" and "(())". &

SRM 628 DIV2

250  想想就发现规律了. 500  暴力,括号匹配. 1000 给一个f数组,如果i存在,那么f[i]也得存在,问这样的集合有多少种. 先拓扑一下,dp[i] = mul(dp[son]+1)最后环里面的元素的乘积是结果. #include <iostream> #include <cstdio> #include <string> #include <algorithm> #include <stdlib.h> #include <v

topcoder SRM 619 DIV2 GoodCompanyDivTwo

注意题目给的最后一句话,如果部门任何employee都做不同类型的工作,则这个部门是一个diverse,题目是计算department的diverse数 读起来感觉有点别扭,英语没学好的原因 int countGood(vector <int> superior, vector <int> workType) { int res = 0; for(int i = 0 ; i < superior.size(); ++ i){ set<int> department

Topcoder SRM 619 DIv2 500 --又是耻辱的一题

这题明明是一个简单的类似约瑟夫环的问题,但是由于细节问题迟迟不能得到正确结果,结果比赛完几分钟才改对..耻辱. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #define ll long long using namespace std; #define NN 370000 class Choo