# Dig a border trench

Background: Too many illegal immigrants from Blandia are crossing the border to Astan. The emperor of Astan has tasked you with digging a trench to keep them out, and Blandia must pay for the expenses. Since all typists have been furloughed until the trench is arranged, your code must be as short as possible.*

Task: Given a 2D map of the border between Astan and Blandia, make the Blands pay (with land) for a border trench.

For example: With Astanian cells marked A, Blandic cells marked B and trench cells marked + (the map frames are only for clarity):

┌──────────┐ ┌──────────┐
│AAAAAAAAAA│ │AAAAAAAAAA│
│ABAAAAAABA│ │A+AAAAAA+A│
│ABBBAABABA│ │A+++AA+A+A│
│ABBBAABABA│ │A+B+AA+A+A│
│ABBBBABABA│→│A+B++A+A+A│
│ABBBBABBBB│ │A+BB+A++++│
│ABBBBABBBB│ │A+BB+A+BBB│
│ABBBBBBBBB│ │A+BB+++BBB│
│BBBBBBBBBB│ │++BBBBBBBB│
└──────────┘ └──────────┘


Details: The map will have at least three rows and three columns. The top row will be entirely Astanian and the bottom row will be entirely Blandic.
You may use any three values to represent Astanian territory, Blandic territory, and border trench, as long as input and output are consistent.

Automaton formulation: A Blandic cell with at least one Astanian cell in its Moore neighbourhood becomes a border trench cell.

# Test cases

[
"AAAAAAAAAA",
"ABAAAAAABA",
"ABBBAABABA",
"ABBBAABABA",
"ABBBBABABA",
"ABBBBABBBB",
"ABBBBABBBB",
"ABBBBBBBBB",
"BBBBBBBBBB"
]


becomes:

[
"AAAAAAAAAA",
"A+AAAAAA+A",
"A+++AA+A+A",
"A+B+AA+A+A",
"A+B++A+A+A",
"A+BB+A++++",
"A+BB+A+BBB",
"A+BB+++BBB",
"++BBBBBBBB"
]


[
"AAA",
"AAA",
"BBB"
]


becomes:

[
"AAA",
"AAA",
"+++"
]


[
"AAAAAAAAAA",
"AAAABBBAAA",
"AAAABBBAAA",
"AAAABBBAAA",
"AAAAAAAAAA",
"BBBBBBABBB",
"BBBBBBAABB",
"BBBAAAAABB",
"BBBBBBBBBB"
]


becomes:

[
"AAAAAAAAAA",
"AAAA+++AAA",
"AAAA+B+AAA",
"AAAA+++AAA",
"AAAAAAAAAA",
"++++++A+++",
"BB++++AA+B",
"BB+AAAAA+B",
"BB+++++++B"
]


* DISCLAIMER: ANY RESEMBLANCE TO ACTUAL GEOPOLITICS IS PURELY COINCIDENTAL!

• Political satire in the form of code golf, I love it :o)
– Sok
Jan 16 '19 at 17:17
• -1 for that <sup><sub><sup><sub><sup><sub><sup><sub> :-P Jan 16 '19 at 19:44
• python, 4 bytes: pass The plans to build a border trench lead to a government shutdown and nothing happens. Jan 16 '19 at 21:43
• @TheEspinosa No no, the shutdown is until the trench is arranged.
Jan 16 '19 at 21:45
• I upvoted just because of the background story. Didn't even continue reading.
– pipe
Jan 17 '19 at 20:16

# Wolfram Language (Mathematica), 15 bytes

2#-#~Erosion~1&


Try it online!

Or (39 bytes):

MorphologicalPerimeter[#,Padding->1]+#&


Try it online!

What else would we expect from Mathematica? Characters used are {Astan -> 0, Blandia -> 1, Trench -> 2}.

# MATL, 11 8 bytes

EG9&3ZI-


The input is a matrix with Astan represented by 0 and Blandia by 1. Trench is represented in the output by 2.

Try it online!

### How it works

E       % Implicit input. Multiply by 2, element-wise
G       % Push input again
9       % Push 9
&3ZI    % Erode with neighbourhood of 9 elements (that is, 3×3)
-       % Subtract, element-wise. Implicit display


# JavaScript (ES7),  84  82 bytes

Saved 2 bytes thanks to @Shaggy

Takes input as a matrix of integers, with $$\3\$$ for Astan and $$\0\$$ for Blandia. Returns a matrix with the additional value $$\1\$$ for the trench.

a=>(g=x=>a.map(t=(r,Y)=>r.map((v,X)=>1/x?t|=(x-X)**2+(y-Y)**2<v:v||g(X,y=Y)|t)))()


Try it online!

### Commented

a => (                      // a[] = input matrix
g = x =>                  // g = function taking x (initially undefined)
a.map(t =               //   initialize t to a non-numeric value
(r, Y) =>             //   for each row r[] at position Y in a[]:
r.map((v, X) =>       //     for each value v at position X in r[]:
1 / x ?             //       if x is defined (this is a recursive call):
t |=              //         set the flag t if:
(x - X) ** 2 +  //           the squared Euclidean distance
(y - Y) ** 2    //           between (x, y) and (X, Y)
< v             //           is less than v (3 = Astan, 0 = Blandia)
:                   //       else (this is the initial call to g):
v ||              //         yield v unchanged if it's equal to 3 (Astan)
g(X, y = Y)       //         otherwise, do a recursive call with (X, Y) = (x, y)
| t               //         and yield the flag t (0 = no change, 1 = trench)
)                     //     end of inner map()
)                       //   end of outer map()
)()                         // initial call to g

• 82 bytes? Jan 16 '19 at 17:28
• @Shaggy Not enough questions lately. I don't know how to golf anymore. :D Thanks! Jan 16 '19 at 17:32
• I was only thinking the same thing earlier! Jan 16 '19 at 17:32

# K (ngn/k), 24 bytes

{x+x&2{++/'3':0,x,0}/~x}


Try it online!

uses 0 1 2 for "AB+"

{ } function with argument x

~ logical not

2{ }/ twice do

• 0,x,0 surround with 0-s (top and bottom of the matrix)

• 3': triples of consecutive rows

• +/' sum each

• + transpose

x& logical and of x with

x+ add x to

# APL (Dyalog Unicode), 11 bytesSBCS

⊢⌈{2∊⍵}⌺3 3


this is based on @dzaima's 12-byte solution in chat. credit to @Adám himself for thinking of using ∊ in the dfn, and to @H.PWiz for reminding us to use the same encoding for input and output

Try it online!

represents 'AB+' as 2 0 1 respectively

{ }⌺3 3 apply a function to each overlapping 3×3 region of the input, including regions extending 1 unit outside the matrix, padded with 0s

2∊⍵ is a 2 present in the argument? return a 0/1 boolean

⊢⌈ per-element max of that and the original matrix

• Of course, switching to Stencil would save you more than half of your bytes.
Jan 16 '19 at 22:22
• @Adám that would be an answer in a different language, so not comparable or competing against this answer. and i don't find golfing in special-purpose languages particularly interesting, sorry
– ngn
Jan 16 '19 at 23:24
• @Adám an alias for display which i forgot to remove. removed now
– ngn
Jan 17 '19 at 2:12

# PowerShell, 220 bytes

It's not as small as the other submissions, but I thought I'd add it for reference. [FORE!]

function m($i,$m){($i-1)..($i+1)|?{$_-in0..$m}}
$h=$a.count-1;$w=$a[0].length-1
foreach($i in 0..$h){-join$(foreach($j in 0..$w){if ($a[$i][$j]-eq'B'-and($a[(m$i $h)]|?{$_[(m $j$w)]-match'A'})){'+'}else{$a[$i][$j]}})}  Try it online! • Welcome to PPCG. Nice first answer, with TIO link even! Don't worry about code length; each language competes against itself. Btw, you can save one byte by removing the first line break with no ill effects. – Adám Jan 20 '19 at 20:35 • Can the final line become 0..$h|%{-join$(foreach($j in 0..$w){if ($a[$_][$j]-eq'B'-and($a[(m$_ $h)]|?{$_[(m $j$w)]-match'A'})){'+'}else{$a[$_][$j]}})} for 207 bytes? Jan 20 '19 at 22:09 # Octave, 37 31 26 bytes This function performs a morphological erosion on the Astan (1-b) part of the "image" using conv2 imerode, and then uses some arithmetic to make all three areas different symbols. Thanks @LuisMendo for -5 bytes! @(b)2*b-imerode(b,ones(3))  Try it online! • -1 for no convolution :-P Jan 16 '19 at 19:46 • @LuisMendo An earlier version did include a convolution:) Jan 16 '19 at 20:59 • Thanks, updated! Jan 17 '19 at 9:15 # J, 28 bytes >.3 3(2 e.,);._3(0|:@,|.)^:4  Try it online! 'AB+' -> 2 0 1 Inspired by ngn's APL solution. 12 bytes just to pad the matrix with zeroes... • why is the APL solution able to get away without doing the zero padding? Jan 26 '19 at 19:19 • @Jonah: APL's ⌺ (Stencil) does it atomatically: "Rectangles are centred on successive elements of Y and (unless the rectangle size is 1), padded with fill elements." Jan 27 '19 at 7:57 • that seems so much more useful than J’s version... Jan 27 '19 at 15:41 • @Jonah Yes, it is! Jan 27 '19 at 19:29 # Charcoal, 20 bytes ≔⪫θ⸿θＰθＦθ⎇∧№ＫＭA⁼Bι+ι  Try it online! Link is to verbose version of code. Explanation: ≔⪫θ⸿θ  Join the input array with carriage returns rather than the usual newlines. This is needed so that the characters can be printed individually. Ｐθ  Print the input string without moving the cursor. Ｆθ  Loop over each character of the input string. ⎇∧№ＫＭA⁼Bι  If the Moore neighbourhood contains an A, and the current character is a B... +  ... then overwrite the B with a +... ι  ... otherwise print the current character (or move to the next line if the current character is a carriage return). # JavaScript, 85 bytes Threw this together late last night and forgot about it. Probably still room for some improvement somewhere. Input and output is as an array of digit arrays, using 3 for Astan, 0 for Blandia & 1 for the trench. a=>a.map((x,i)=>x.map((y,j)=>o.map(v=>o.map(h=>y|=1&(a[i+v]||x)[j+h]))|y),o=[-1,0,1])  Try it online (For convenience, maps from & back to the I/O format used in the challenge) # Javascript, 126 118 bytes _=>_.map(x=>x.replace(/(?<=A)B|B(?=A)/g,0)).map((x,i,a)=>[...x].map((v,j)=>v>'A'&&(a[i-1][j]<v|(a[i+1]||x)[j]<v)?0:v))  Pass in one of the string arrays from the question, and you'll get an array of strings character arrays (thanks @Shaggy!) out using 0 for the trench. Can probably be golfed more (without switching over to numerical arrays), but I can't think of anything at the moment. • I think this works for 120 bytes. Jan 18 '19 at 12:43 • Or 116 bytes returning an array of character arrays. Jan 18 '19 at 12:43 • @Shaggy Your golf doesn't work, sadly - it doesn't catch places where A's and B's are diagonal to each-other. On the other hand, it does point out some really simple golfs that I missed... Jan 18 '19 at 15:07 # Retina 0.8.2, 92 80 bytes (?<=¶(.)*)B(?=.*¶(?<-1>.)*(?(1)_)A|(?<=¶(?(1)_)(?<-1>.)*A.*¶.*)) a iTBa+.?a.?  Try it online! Loosely based on my answer to Will I make it out in time? Explanation: Any Bs immediately above or below As are turned into as. This then reduces the problem to checking Bs to the left or right of As or as. The as themselves also need to get turned into +s of course, but fortunately the i flag to T only affects the regex match, not the actual transliteration, so the As remain unaffected. # 05AB1E, 29 bytes _2FIн¸.øVgN+FYN._3£})εøO}ø}*Ā+  Matrices aren't really 05AB1E's strong suit (nor are they my strong suit).. Can definitely be golfed some more, though. Inspired by @ngn's K (ngn/k) answer, so also uses I/O of a 2D integer matrix with 012 for AB+ respectively. Try it online. (The footer in the TIO is to pretty-print the output. Feel free to remove it to see the matrix output.) Explanation: _ # Inverse the values of the (implicit) input-matrix (0→1 and 1→0) # i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]] # → [[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0]] 2F # Loop n 2 times in the range [0, 2): Iн # Take the input-matrix, and only leave the first inner list of 0s # i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]] → [0,0,0,0] ¸ # Wrap it into a list # i.e. [0,0,0,0] → [[0,0,0,0]] .ø # Surround the inverted input with the list of 0s # i.e. [[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0]] and [0,0,0,0] # → [[0,0,0,0],[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0]] V # Pop and store it in variable Y g # Take the length of the (implicit) input-matrix # i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]] → 4 N+ # Add n to it # i.e. 4 and n=0 → 4 # i.e. 4 and n=1 → 5 F # Inner loop N in the range [0, length+n): Y # Push matrix Y N._ # Rotate it N times towards the left # i.e. [[0,0,0,0],[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0]] and N=2 # → [[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,1,1,1]] 3£ # And only leave the first three inner lists # i.e. [[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,1,1,1]] # → [[1,0,1,1],[1,0,0,0],[0,0,0,0]] } # After the inner loop: ) # Wrap everything on the stack into a list # → [[[0,0,0,0],[1,1,1,1],[1,0,1,1]],[[1,1,1,1],[1,0,1,1],[1,0,0,0]],[[1,0,1,1],[1,0,0,0],[0,0,0,0]],[[1,0,0,0],[0,0,0,0],[0,0,0,0]]] €ø # Zip/transpose (swapping rows/columns) each matrix in the list # → [[[0,1,1],[0,1,0],[0,1,1],[0,1,1]],[[1,1,1],[1,0,0],[1,1,0],[1,1,0]],[[1,1,0],[0,0,0],[1,0,0],[1,0,0]],[[1,0,0],[0,0,0],[0,0,0],[0,0,0]]] O # And take the sum of each inner list # → [[2,1,2,2],[3,1,2,2],[2,0,1,1],[1,0,0,0]] ø # Zip/transpose; swapping rows/columns the entire matrix again # i.e. [[2,1,2,2],[3,1,2,2],[2,0,1,1],[1,0,0,0]] # → [[2,3,2,1],[1,1,0,0],[2,2,1,0],[2,2,1,0]] } # After the outer loop: # i.e. [[3,5,5,4,2],[4,6,5,4,2],[2,3,2,2,1],[1,1,0,0,0]] * # Multiple each value with the input-matrix at the same positions, # which implicitly removes the trailing values # i.e. [[3,5,5,4,2],[4,6,5,4,2],[2,3,2,2,1],[1,1,0,0,0]] # and [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]] # → [[0,0,0,0],[0,1,0,0],[0,2,1,0],[2,2,1,0]] Ā # Truthify each value (0 remains 0; everything else becomes 1) # i.e. [[0,0,0,0],[0,1,0,0],[0,2,1,0],[2,2,1,0]] # → [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,0,0]] + # Then add each value with the input-matrix at the same positions # i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,0,0]] # and [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]] # → [[0,0,0,0],[0,2,0,0],[0,2,2,2],[2,2,1,1]] # (and output the result implicitly)  # C# (Visual C# Interactive Compiler), 187 bytes a=>a.Select((b,i)=>b.Select((c,j)=>{int k=0;for(int x=Math.Max(0,i-1);x<Math.Min(i+2,a.Count);x++)for(int y=Math.Max(0,j-1);y<Math.Min(j+2,a[0].Count);)k+=a[x][y++];return k>1&c<1?9:c;}))  Instead of chaining Take()s, Skip()s, and Select()s, instead this uses double for loops to find neighbors. HUGE byte decrease, from 392 bytes to 187. Linq isn't always the shortest! Try it online! # Perl 5, 58 46 bytes $m=($n=/$/m+"@+")-2;s/A(|.{$m,$n})\KB|B(?=(?1)A)/+/s&&redo


TIO

-12 bytes thanks to @Grimy

/.
/;s/A(|.{@{-}}.?.?)\KB|B(?=(?1)A)/+/s&&redo


TIO

• -p like -n but print also
• -00 paragraph mode
• to get the width-1 /.\n/ matches the last character of first line
• @{-} special array the position of start of match of previous matched groups, coerced as string (first element)
• s/../+/s&&redo replace match by + while matches
• /s flag, so that . matches also newline character
• A(|.{@{-}}.?.?)\KB matches
• AB or A followed by (width-1) to (width+1) characters folowed by B
• \K to keep the left of B unchanged
• B(?=(?1)A),
• (?1) dirverting recursive, to reference previous expression (|.{$m,$o})
• (?=..) lookahead, to match without consuming input
• -9 bytes with /. /,@m=@-while s/A(|.{@m}.?.?)\KB|B(?=(?1)A)/+/s (literal newline in the first regex). TIO Jan 18 '19 at 10:32
• Down to 46: /. /;s/A(|.{@{-}}.?.?)\KB|B(?=(?1)A)/+/s&&redo. TIO Jan 18 '19 at 10:50
• thanks, i also had the idea, but discarded because was thinking to catastrophic backtracking however for code golf performance is not important Jan 18 '19 at 11:14

# Java 8, 169 145 bytes

m->{for(int i=m.length,j,k;i-->0;)for(j=m[i].length;j-->0;)for(k=9;m[i][j]==1&k-->0;)try{m[i][j]=m[i+k/3-1][j+k%3-1]<1?2:1;}catch(Exception e){}}


-24 bytes thanks to @OlivierGrégoire.

Uses 0 instead of A and 1 instead of B, with the input being a 2D integer-matrix. Modifies the input-matrix instead of returning a new one to save bytes.

The cells are checked the same as in my answer for the All the single eights challenge.

Try it online.

Explanation:

m->{                            // Method with integer-matrix parameter and no return-type
for(int i=m.length,j,k;i-->0;)//  Loop over the rows
for(j=m[i].length;j-->0;)   //   Inner loop over the columns
for(k=9;m[i][j]==1&       //    If the current cell contains a 1:
k-->0;)               //     Inner loop k in the range (9, 0]:
try{m[i][j]=            //      Set the current cell to:
m[i+k/3-1]         //       If k is 0, 1, or 2: Look at the previous row
//       Else-if k is 6, 7, or 8: Look at the next row
//       Else (k is 3, 4, or 5): Look at the current row
[j+k%3-1]         //       If k is 0, 3, or 6: Look at the previous column
//       Else-if k is 2, 5, or 8: Look at the next column
//       Else (k is 1, 4, or 7): Look at the current column
<1?              //       And if this cell contains a 0:
2             //        Change the current cell from a 1 to a 2
:              //       Else:
1;            //        Leave it a 1
}catch(Exception e){}}  //      Catch and ignore ArrayIndexOutOfBoundsExceptions
//      (try-catch saves bytes in comparison to if-checks)

• I haven't checked much, but is there anything wrong with m[i+k/3-1][j+k%3-1]? 145 bytes Jan 18 '19 at 12:31
• @OlivierGrégoire Dang, that's so much easier.. Thanks! Jan 18 '19 at 12:44
• I think it's also valid for your answers of previous challenges given that they seem to have the same structure Jan 18 '19 at 12:51
• @OlivierGrégoire Yeah, I was about to golf those as well with your suggestion, but then another comment (and a question at work) came in between. Will do so in a moment. Jan 18 '19 at 13:00

# PowerShell, 86 80 bytes

$p="(.?.?.{$(($args|% i*f ' ')-1)})?"$args-replace"(?s)(?<=A$p)B|B(?=$pA)",'+'


Try it online!

The map is a string with newlines. This script replaces B to + with regexp (?<=A(.?.?.{$MapWidth-1})?)B|B(?=(.?.?.{$MapWidth-1})?A).

Less golfed Test script:

$f = {$l=($args|% indexOf "n")-1$p="(.?.?.{$l})?"$args-replace"(?s)(?<=A$p)B|B(?=$pA)",'+'
}

@(
,(@"
AAAAAAAAAA
ABAAAAAABA
ABBBAABABA
ABBBAABABA
ABBBBABABA
ABBBBABBBB
ABBBBABBBB
ABBBBBBBBB
BBBBBBBBBB
"@,@"
AAAAAAAAAA
A+AAAAAA+A
A+++AA+A+A
A+B+AA+A+A
A+B++A+A+A
A+BB+A++++
A+BB+A+BBB
A+BB+++BBB
++BBBBBBBB
"@)
,(@"
AAA
AAA
BBB
"@,@"
AAA
AAA
+++
"@)
,(@"
AAAAAAAAAA
AAAABBBAAA
AAAABBBAAA
AAAABBBAAA
AAAAAAAAAA
BBBBBBABBB
BBBBBBAABB
BBBAAAAABB
BBBBBBBBBB
"@,@"
AAAAAAAAAA
AAAA+++AAA
AAAA+B+AAA
AAAA+++AAA
AAAAAAAAAA
++++++A+++
BB++++AA+B
BB+AAAAA+B
BB+++++++B
"@)
) | % {
$map,$expected = $_$result = &$f$map
$result-eq$expected
#$result # uncomment this line to display results }  Output: True True True  # Ruby, 102 bytes ->a{a+=?.*s=a.size (s*9).times{|i|a[j=i/9]>?A&&a[j-1+i%3+~a.index($/)*(i/3%3-1)]==?A&&a[j]=?+}
a[0,s]}


Try it online!

input/output as a newline separated string

# Python 2, 123 119 bytes

lambda m:[[[c,'+'][c=='B'and'A'in[x[j-(j>0):j+2]for x in m[i-(i>0):i+2]]]for j,c in e(l)]for i,l in e(m)];e=enumerate


Try it online!

I/O is a list of lists

# TSQL, 252 bytes

Splitting the string is very costly, if the string was split and already in a table the byte count would be 127 characters. Script included in the bottom and completely different. Sorry for taking up this much space.

Golfed:

WITH C as(SELECT x,x/z r,x%z c,substring(@,x+1,1)v
FROM spt_values CROSS APPLY(SELECT number x,charindex(char(10),@)z)z
WHERE'P'=type)SELECT @=stuff(@,x+1,1,'+')FROM c WHERE
exists(SELECT*FROM c d WHERE abs(r-c.r)<2and
abs(c-c.c)<2and'AB'=v+c.v)PRINT @


Ungolfed:

DECLARE @ varchar(max)=
'AAAAAAAAAA
ABAAAAAABA
ABBBAABABA
ABBBAABABA
ABBBBABABA
ABBBBABBBB
ABBBBABBBB
ABBBBBBBBB
BBBBBBBBBB';

WITH C as
(
SELECT x,x/z r,x%z c,substring(@,x+1,1)v
FROM spt_values
CROSS APPLY(SELECT number x,charindex(char(10),@)z)z
WHERE'P'=type
)
SELECT
@=stuff(@,x+1,1,'+')
FROM c
WHERE exists(SELECT*FROM c d
WHERE abs(r-c.r)<2
and abs(c-c.c)<2 and'AB'=v+c.v)
PRINT @


Try it out

# TSQL, 127 bytes(Using table variable as input)

Execute this script in management studio - use "query"-"result to text" to make it readable

--populate table variable
USE master
DECLARE @v varchar(max)=
'AAAAAAAAAA
ABAAAAAABA
ABBBAABABA
ABBBAABABA
ABBBBABABA
ABBBBABBBB
ABBBBABBBB
ABBBBBBBBB
BBBBBBBBBB'

DECLARE @ table(x int, r int, c int, v char)

INSERT @
SELECT x+1,x/z,x%z,substring(@v,x+1,1)
FROM spt_values
CROSS APPLY(SELECT number x,charindex(char(10),@v)z)z
WHERE'P'=type and len(@v)>number

-- query(127 characters)

SELECT string_agg(v,'')FROM(SELECT
iif(exists(SELECT*FROM @
WHERE abs(r-c.r)<2and abs(c-c.c)<2and'AB'=v+c.v),'+',v)v
FROM @ c)z


Try it out - warning output is selected and not readable. Would be readable with print, but that is not possible using this method

• What makes you think that you can't take a table as argument?