> For the complete documentation index, see [llms.txt](https://xtecna.gitbook.io/solucoes-da-beecrowd/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://xtecna.gitbook.io/solucoes-da-beecrowd/ad-hoc/1618-colisao.md).

# 1618 - Colisão

## Descrição

{% embed url="<https://www.urionlinejudge.com.br/judge/pt/problems/view/1618>" %}

## Solução

O problem setter deste problema fez um excelente trabalho em nos trazer uma ilustração que nos explica o problema a ser resolvido e nos dá uma luz em como resolvê-lo. Pelo desenho, podemos perceber as seguintes coordenadas dos pontos apresentados:

| Pontos | Coordenadas |
| ------ | ----------- |
| A      | (4, 4)      |
| B      | (9, 4)      |
| C      | (9, 8)      |
| D      | (4, 8)      |
| Robô   | (6, 5)      |

{% hint style="warning" %}
Os pontos A, B, C e D nem sempre formam um retângulo certinho, é nosso papel avaliar se é possível que o robô esteja dentro seguindo todas as regras abaixo. Dessa maneira, também podemos avaliar ao mesmo tempo se os pontos formam um retângulo ou não.
{% endhint %}

Com isso, para o robô estar dentro do espaço delimitado por esses pontos, vamos pensar separadamente: primeiro na coordenada x do robô e depois na coordenada y.

Pela coordenada x do robô, podemos ver que tal coordenada precisa estar entre a coordenada x de A e D e a coordenada x de B e C. Se essa coordenada for menor que a de A ou a de D, o robô sempre está do lado de fora. Pelo mesmo raciocínio, se essa coordenada for maior que a de B ou a de C, o robô também está do lado de fora.

Pela coordenada y do robô, o raciocínio é semelhante, onde tal coordenada precisa estar entre a coordenada y de A e B e a coordenada y de D e C.

Em termos matemáticos, vamos testar

$$
A\_{x} \leq R\_{x}, D\_{x} \leq R\_{x}, R\_{x} \leq B\_{x}, R\_{x} \leq C\_{x} \\
\text{e} \\
A\_{y} \leq R\_{y}, B\_{y} \leq R\_{y}, R\_{y} \leq C\_{y}, R\_{y} \leq D\_{y}
$$

{% tabs %}
{% tab title="C99" %}

```c
#include <stdio.h>

int main()
{
    int N, Ax, Ay, Bx, By, Cx, Cy, Dx, Dy, Rx, Ry;

    scanf("%d", &N);

    for (int i = 0; i < N; ++i)
    {
        scanf("%d %d %d %d %d %d %d %d %d %d", &Ax, &Ay, &Bx, &By, &Cx, &Cy, &Dx, &Dy, &Rx, &Ry);

        printf("%d\n", Ax <= Rx && Dx <= Rx &&
                           Rx <= Bx && Rx <= Cx &&
                           Ay <= Ry && By <= Ry &&
                           Ry <= Cy && Ry <= Dy);
    }

    return 0;
}
```

{% endtab %}

{% tab title="C++17" %}

```cpp
#include <iostream>

using namespace std;

int main()
{
    int N, Ax, Ay, Bx, By, Cx, Cy, Dx, Dy, Rx, Ry;

    cin >> N;

    for (int i = 0; i < N; ++i)
    {
        cin >> Ax >> Ay >> Bx >> By >> Cx >> Cy >> Dx >> Dy >> Rx >> Ry;

        cout << (int)(Ax <= Rx && Dx <= Rx &&
                      Rx <= Bx && Rx <= Cx &&
                      Ay <= Ry && By <= Ry &&
                      Ry <= Cy && Ry <= Dy)
             << endl;
    }

    return 0;
}
```

{% endtab %}

{% tab title="JavaScript 12.18" %}

```javascript
var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');

let N = parseInt(lines.shift().trim());

for(let i = 0; i < N; ++i){
    let [Ax, Ay, Bx, By, Cx, Cy, Dx, Dy, Rx, Ry] = lines.shift().trim().split(' ').map((x) => parseInt(x));

    console.log(Ax <= Rx && Dx <= Rx &&
                Rx <= Bx && Rx <= Cx &&
                Ay <= Ry && By <= Ry &&
                Ry <= Cy && Ry <= Dy ? 1 : 0);
}
```

{% endtab %}

{% tab title="Python 3.9" %}

```python
N = int(input())

for _ in range(N):
    Ax, Ay, Bx, By, Cx, Cy, Dx, Dy, Rx, Ry = [
        int(x) for x in input().strip().split(' ')]

    print(1 if Ax <= Rx <= Bx and Dx <= Rx <=
          Cx and Ay <= Ry <= Dy and By <= Ry <= Cy else 0)
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xtecna.gitbook.io/solucoes-da-beecrowd/ad-hoc/1618-colisao.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
