1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

LINQで7クイーン(書類選考無し&高級うなぎGET)

Posted at

問題はこちら
実行結果はこちら

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;

struct Queen
{
  public int X;
  public int Y;
  public Queen(int x, int y)
  {
    this.X = x;
    this.Y = y;
  }
  public bool IsMovable(Queen q)
  {
    return (this.X == q.X) || (this.Y == q.Y) || this.IsDiagonal(q);
  }
  public bool IsDiagonal(Queen q)
  {
    var diffX = this.X - q.X;
    var diffY = this.Y - q.Y;
    return (diffX == diffY || diffX == -diffY);
  }
}

class Program
{
  static void Main()
  {
    var letters = new string[]
    {
      "lhikoav",
      "rqsczlp",
      "uwalnfo",
      "tykajeh",
      "ahitsyd",
      "efoptxn",
      "ruzwyve"
    };
    var result =
      from q1 in Enumerable.Range(1, 7).Select(x => new Queen(x, 1))
      from q2 in Enumerable.Range(1, 7).Select(x => new Queen(x, 2))
      from q3 in new Queen[] {new Queen(3, 3)}
      from q4 in Enumerable.Range(1, 7).Select(x => new Queen(x, 4))
      from q5 in new Queen[] {new Queen(4, 5)}
      from q6 in Enumerable.Range(1, 7).Select(x => new Queen(x, 6))
      from q7 in Enumerable.Range(1, 7).Select(x => new Queen(x, 7))
      let queens = new Queen[] {q1, q2, q3, q4, q5, q6, q7}
      where Combinations(queens).All(t => !t[0].IsMovable(t[1]))
      select new string((from q in queens
                         orderby q.X * q.Y
                         select letters[q.Y - 1][q.X - 1])
                        .ToArray());
    foreach (var s in result)
      Console.WriteLine(s);
  }

  static IEnumerable<T[]> Combinations<T>(IEnumerable<T> source)
  {
    var list = new List<T>(source);
    for (var i = 0; i < list.Count; i++)
      for (var j = i + 1; j < list.Count; j++)
        yield return new T[] {list[i], list[j]};
  }
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?