第一次做TC,不太习惯,各种调试,只做了一题。。。。。。
Problem Statement
    Fox Ciel is going to play Gomoku with her friend Fox Jiro. Ciel plays better, so before they start she allowed Jiro to put some of his pieces on the board.
You are given a vector <string> board that represents a square board. The character board[j] represents the cell with coordinates (i,j). Each of those characters is either '.' (representing an empty cell) or 'o' (representing a cell with Jiro's piece).
Of course, Ciel does not want Jiro to win the game before she has a chance to play. Thus she now has to check the board and determine whether there are five consecutive tokens somewhere on the board.
Determine whether there are 5 consecutive cells (horizontally, vertically, or diagonally) that contain Jiro's tokens. Return "found" (quotes for clarity) if there are five such cells anywhere on the board. Otherwise, return "not found".
Definition
 
Class:FoxAndGomoku
Method:win
Parameters:vector <string>
Returns:string
Method signature:string win(vector <string> board)
(be sure your method is public)
    
Constraints
-n will be between 5 and 15, inclusive.
-board will contain exactly n elements.
-Each element in board will contain exactly n characters.
-Each character in board will be 'o' or '.'.
Examples
0)
   
{"....o.",
 "...o..",
 "..o...",
 ".o....",
 "o.....",
 "......"}
Returns: "found"
There is five continue pieces diagonally.
1)
   
{"oooo.",
 ".oooo",
 "oooo.",
 ".oooo",
 "....."}
 
Returns: "not found"
There is no five-in-a-row on this board.
2)
   
{"oooo.",
 ".oooo",
 "oooo.",
 ".oooo",
 "....o"}
 
Returns: "found"
Five consecutive tokens can be found in the following cells: (0,0), (1,1), (2,2), (3,3), and (4,4).
3)
   
{"o.....",
 ".o....",
 "..o...",
 "...o..",
 "....o.",
 "......"}
Returns: "found"
4)
   
{"o....",
 "o.o..",
 "o.o.o",
 "o.o..",
 "o...."}
Returns: "found"
5)
   
{"..........",
 "..........",
 "..oooooo..",
 "..o.......",
 "..o.......",
 "..oooooo..",
 ".......o..",
 ".......o..",
 "..oooooo..",
 ".........."}
 
Returns: "found"
6)
   
{"..........",
 "..........",
 "..oooo.o..",
 "..o.......",
 "..o.......",
 "..o.oooo..",
 ".......o..",
 ".......o..",
 "..oooo.o..",
 ".........."}
Returns: "not found"
7)
   
{"ooooo",
 "ooooo",
 "ooooo",
 "ooooo",
 "ooooo"}
Returns: "found"
8)
   
{".....",
 ".....",
 ".....",
 ".....",
 "....."}
Returns: "not found"
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

// BEGIN CUT HERE

// END CUT HERE
#line 5 "FoxAndGomoku.cpp"
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <ctime>
using namespace std;
typedef long long ll;

int n,m;
bool vis[20][20],flag=false;
int dir_x[8]={1,-1,0,0,-1,-1,1,1};
int dir_y[8]={0,0,1,-1,1,-1,1,-1};
int X,Y;

bool inmap(int x,int y)
{
    if(x>=0&&x<n&&y>=0&&y<n) return true;
    return false;
}

void dfs(int x,int y,int tot,vector<string> &board,int dir)
{
    if(flag) return ;
    if(tot>=4) { flag=true; return ; }
    if(dir==-1)
    {
        for(int i=0;i<8;i++)
        {
            int X=x+dir_x;
            int Y=y+dir_y;
            if(inmap(X,Y))
            {
                if(board[X][Y]=='o'&&vis[X][Y]==false)
                {
                    vis[X][Y]=true;
                    dfs(X,Y,tot+1,board,i);
                    vis[X][Y]=false;
                }
            }
        }
    }
    else
    {
        int X=x+dir_x[dir];
        int Y=y+dir_y[dir];
        if(inmap(X,Y))
        {
            if(board[X][Y]=='o'&&vis[X][Y]==false)
            {
                vis[X][Y]=true;
                dfs(X,Y,tot+1,board,dir);
                vis[X][Y]=false;
            }
        }
    }
    return ;
}

class FoxAndGomoku
{
        public:
        string win(vector <string> board)
        {
            int i,j,k;
            n=board.size();
            vector<int> vt;
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<n;j++)
                {
                    if(board[j]=='o')
                        vt.push_back(i*n+j);
                }
            }

flag=false;
            for(int i=0;i<vt.size();i++)
            {
                int x,y;
                int u=vt;
                x=u/n; y=u%n;
                memset(vis,false,sizeof(vis));
                vis[x][y]=true;
                dfs(x,y,0,board,-1);
                if(flag)
                    break;
            }
            string st;
            if(flag)
                st="found";
            else
                st="not found";
            return st;
        }

// 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(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); if ((Case == -1) || (Case == 6)) test_case_6(); if ((Case == -1) || (Case == 7)) test_case_7(); if ((Case == -1) || (Case == 8)) test_case_8(); }
    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 string &Expected, const string &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() { string Arr0[] = {"....o.",
 "...o..",
 "..o...",
 ".o....",
 "o.....",
 "......"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "found"; verify_case(0, Arg1, win(Arg0)); }
    void test_case_1() { string Arr0[] = {"oooo.",
 ".oooo",
 "oooo.",
 ".oooo",
 "....."}
 ; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "not found"; verify_case(1, Arg1, win(Arg0)); }
    void test_case_2() { string Arr0[] = {"oooo.",
 ".oooo",
 "oooo.",
 ".oooo",
 "....o"}
 ; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "found"; verify_case(2, Arg1, win(Arg0)); }
    void test_case_3() { string Arr0[] = {"o.....",
 ".o....",
 "..o...",
 "...o..",
 "....o.",
 "......"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "found"; verify_case(3, Arg1, win(Arg0)); }
    void test_case_4() { string Arr0[] = {"o....",
 "o.o..",
 "o.o.o",
 "o.o..",
 "o...."}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "found"; verify_case(4, Arg1, win(Arg0)); }
    void test_case_5() { string Arr0[] = {"..........",
 "..........",
 "..oooooo..",
 "..o.......",
 "..o.......",
 "..oooooo..",
 ".......o..",
 ".......o..",
 "..oooooo..",
 ".........."}

; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "found"; verify_case(5, Arg1, win(Arg0)); }
    void test_case_6() { string Arr0[] = {"..........",
 "..........",
 "..oooo.o..",
 "..o.......",
 "..o.......",
 "..o.oooo..",
 ".......o..",
 ".......o..",
 "..oooo.o..",
 ".........."}
; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "not found"; verify_case(6, Arg1, win(Arg0)); }
    void test_case_7() { string Arr0[] = {"ooooo",
 "ooooo",
 "ooooo",
 "ooooo",
 "ooooo"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "found"; verify_case(7, Arg1, win(Arg0)); }
    void test_case_8() { string Arr0[] = {".....",
 ".....",
 ".....",
 ".....",
 "....."}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "not found"; verify_case(8, Arg1, win(Arg0)); }

// END CUT HERE

};
// BEGIN CUT HERE
int main()
{
        FoxAndGomoku ___test;
        ___test.run_test(-1);
        return 0;
}
// END CUT HERE

* This source code was highlighted by YcdoiT. ( style: Codeblocks )

TopCoder SRM 590的更多相关文章

  1. topcoder srm 590 div1 (max_flow_template)

    problem1 link 对于每一个,找到其在目标串中的位置,判断能不能移动即可. problem2 link 如果最后的$limit$为$11=(1011)_{2}$,那么可以分别计算值为$(10 ...

  2. Topcoder SRM 590 Fox And City

    Link 注意到原图给的是一个无向连通图. 如果在原图中两点之间有一条无向边,那么这两点到\(1\)的距离之差不大于\(1\). 这个命题的正确性是显然的,我们考虑它的逆命题: 给定每个点到\(1\) ...

  3. TopCoder SRM 560 Div 1 - Problem 1000 BoundedOptimization & Codeforces 839 E

    传送门:https://284914869.github.io/AEoj/560.html 题目简述: 定义"项"为两个不同变量相乘. 求一个由多个不同"项"相 ...

  4. Topcoder SRM 643 Div1 250<peter_pan>

    Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*... ...

  5. Topcoder Srm 726 Div1 Hard

    Topcoder Srm 726 Div1 Hard 解题思路: 问题可以看做一个二分图,左边一个点向右边一段区间连边,匹配了左边一个点就能获得对应的权值,最大化所得到的权值的和. 然后可以证明一个结 ...

  6. TopCoder SRM 667 Div.2题解

    概览: T1 枚举 T2 状压DP T3 DP TopCoder SRM 667 Div.2 T1 解题思路 由于数据范围很小,所以直接枚举所有点,判断是否可行.时间复杂度O(δX × δY),空间复 ...

  7. Topcoder Srm 673 Div2 1000 BearPermutations2

    \(>Topcoder \space Srm \space 673 \space Div2 \space 1000 \space BearPermutations2<\) 题目大意 : 对 ...

  8. Topcoder Srm 671 Div2 1000 BearDestroysDiv2

    \(>Topcoder \space Srm \space 671 \space Div2 \space 1000 \space BearDestroysDiv2<\) 题目大意 : 有一 ...

  9. [topcoder]SRM 646 DIV 2

    第一题:K等于1或者2,非常简单.略.K更多的情况,http://www.cnblogs.com/lautsie/p/4242975.html,值得思考. 第二题:http://www.cnblogs ...

随机推荐

  1. javascript 事件传播与事件冒泡,W3C事件模型

    说实话笔者在才工作的时候就听说了什么"事件冒泡",弄了很久才弄个大概,当时理解意思是子级dom元素和父级dom元素都绑定了相同类型的事件,这时如果子级事件触发了父级也会触发,然后这 ...

  2. 微信小程序内测申请

    想申请微信小程序的内测?别做梦了! 小程序内测是邀请制的,目前就发放了200个内测邀请.正因为稀缺,江湖传言内测资格已经炒到300万(一套房)一个了 但是!!!!你可以先熟悉一下相关资料和文档,下载一 ...

  3. Sql Server 附加没有日志文件的数据库(.mdf)文件方法

    附加数据库,附加的时候会提醒找不到log文件 针对以上现象有两个写法的语句能解决: 写法一: USE MASTER; EXEC sp_detach_db @dbname = 'TestDB'; EXE ...

  4. [BZOJ 2186][Sdoi2008]沙拉公主的困惑(欧拉函数)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2186 分析: 就是要求1~n!中与m!互质的数的个数 首先m!以内的就是φ(m!) 关 ...

  5. NoSQL数据库之国产开源产品:SequoiaDB 分析前言

    随着互联网技术的发展,面对海量数据的存储和分析,传统关系型数据库已经无法满足,由此衍生出一种与关系型数据库区别开的数据库NoSQL(Not Only SQL). 国外做的比较成熟的NoSQL有Mong ...

  6. 第十七课:js数据缓存系统的原理

    这一章主要讲的是jQuery的缓存系统的历史发展,以及他自己的框架的缓存系统的实现.都是源码解析. 我就挑几个重点讲下: (1)jQuery的缓存机制的原理 jQuery的缓存机制实现的原理是在元素中 ...

  7. 读代码之private construtor

    private 构造函数 private修饰构造函数在Singleton设计模式中经常使用.但是今天在读到EntityUtils时,发现这是一个final类.final很好理解:EntityUtils ...

  8. 使用GIT来管理代码的心得

    使用GIT来管理代码,第一步当然就是下载一个GIT客户端(不知道是不是这么叫,但是觉得和客户端的功能差不多).电脑的操作系统是windows7的,所以下的是对应的GIT. 就是这玩意,安装的时候不停的 ...

  9. Sublime Text 3 绝对神器

    距第一篇的开箱水文,已经有4个月的时间了,但因为懒,就没有下文了.终于,今天,我觉得写一篇准技术文章了. 忘记了是怎么开始用的ST,应该是在网上看到别人推荐才用到吧,用了有半年了.在windows下是 ...

  10. hdu4825 字典树 XOR

    用字典树思想来做.对于一个数,给出他的二进制,然后更具二进制建立字典树,然后每次询问的时候的数也给出二进制,如果当前为1,那就向0走,为0,向1走. #include<stdio.h> # ...