美文网首页
1128 N Queens Puzzle(20 分)

1128 N Queens Puzzle(20 分)

作者: zjh3029 | 来源:发表于2018-08-25 17:04 被阅读0次
    #include<iostream>
    #include<vector>
    #include<string>
    #include<map>
    #include<algorithm>
    #include <set>
    using namespace std;
    
    int main()
    {
        int M,N,a;
        cin >> M;
        for (int k = 0; k < M; k++)
        {
            cin >> N;
            vector<int> v(N);
            bool flag = false;
            for (int i = 0; i < N; i++)
            {
                cin >> v[i];
                for (int j = 0; j < i; j++)
                {
                    if (abs(i - j) == abs(v[i] - v[j]) || abs(v[i] - v[j]) == 0)
                    {
                        cout << "NO" << endl;
                        flag = true;
                        while (cin.get()!='\n')//丢弃未输入的数据
                        {
                            cin >> a;
                        }
                        break;
                    }
                }
                if (flag == true)
                    break;
            }
            if (flag == false)  cout << "YES" << endl;
        }
        system("pause");
        return 0;
    }
    
    #include<iostream>
    #include<vector>
    #include<string>
    #include<map>
    #include<algorithm>
    #include <set>
    using namespace std;
    
    int main()
    {
        int M,N;
        cin >> M;
        for (int k = 0; k < M; k++)
        {
            cin >> N;
            vector<int> v(N);
            bool flag = false;
            for (int i = 0; i < N; i++)
            {
                cin >> v[i];
                for (int j = 0; j < i; j++)
                {
                    if (abs(i - j) == abs(v[i] - v[j]) || abs(v[i] - v[j]) == 0)
                    {
                        flag = true;
                        break;
                    }
                }
            }
            if (flag == false)  cout << "YES" << endl;
            else cout << "NO" << endl;
        }
        system("pause");
        return 0;
    }
    
    #include<iostream>
    #include<vector>
    #include<string>
    #include<map>
    #include<algorithm>
    #include <set>
    using namespace std;
    
    
    int main()
    {
        int M,N,a;
        vector<int> v;
    
        cin >> M;
        for (int i = 0; i < M; i++)
        {
            cin >> N;
            v.clear();
            for (int j = 0; j < N; j++)
            {
                cin >> a;
                v.push_back(a);
            }
            bool flag = false;
            for (int i = 0; i < v.size(); i++)
            {
                for (int j = i+1; j < v.size(); j++)
                {
                    if (abs(i - j) == abs(v[i] - v[j]) || abs(v[i]-v[j])==0)
                    {
                        cout << "NO" << endl;
                        //cout << "i=" << i << "  j=" << j << endl;
                        flag = true;
                        break;
                    }
                }
                if (flag==true) break;
            }
            if (flag == false)  cout << "YES" << endl;
        }
        system("pause");
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:1128 N Queens Puzzle(20 分)

          本文链接:https://www.haomeiwen.com/subject/pqugiftx.html